PHP & MySQL - saving and looping problems.
Posted
by R.I.P.coalMINERS
on Stack Overflow
See other posts from Stack Overflow
or by R.I.P.coalMINERS
Published on 2010-04-26T10:18:06Z
Indexed on
2010/04/26
10:23 UTC
Read the original article
Hit count: 199
I'm new to PHP and MySQL I want a user to be able to store multiple names and there meanings in a MySQL database tables named names
using PHP I will dynamically create form fields with JQuery every time a user clicks on a link so a user can enter 1 to 1,000,000 different names and there meanings which will be stored in a table called names
.
Since I asked my last question I figured out how to store my values from my form using the for
loop but every time I loop my values when I add one or more dynamic fields the second form field named meaning
will not save the value entered also my dynamic form fields keep looping doubling, tripling and so on the entered values into the database it all depends on how many form fields are added dynamically. I was wondering how can I fix these problems?
On a side note I replaced the query with echo's to see the values that are being entered.
Here is the PHP code.
<?php
if(isset($_POST['submit'])) {
$mysqli = mysqli_connect("localhost", "root", "", "site");
$dbc = mysqli_query($mysqli,"SELECT * FROM names WHERE userID='$userID'");
$name = $_POST['name'];
$meaning = $_POST['meaning'];
if(isset($name['0']) && mysqli_num_rows($dbc) == 0 && trim($name['0'])!=='' && trim($meaning['0'])!=='') {
for($n = 0; $n < count($name); $n++) {
for($m = 0; $m < count($meaning); $m++) {
echo $name[$n] . '<br />';
echo $meaning[$m] . '<br /><br />';
break;
}
}
}
}
?>
And here is the HTML code.
<form method="post" action="index.php">
<ul>
<li><label for="name">Name: </label><input type="text" name="name[]" id="name" /></li>
<li><label for="meaning">Meaning: </label><input type="text" name="meaning[]" id="meaning" /></li>
<li><input type="submit" name="submit" value="Save" /></li>
</ul>
</form>
If needed I will place the JQuery code.
© Stack Overflow or respective owner