开发者

PHP & MySQL - saving and looping problems

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: </l开发者_开发百科abel><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.


If you take a look at the submitted array, things might get clearer:

echo('<pre>' . print_r($_POST, TRUE) . '</pre>');

In case you're still not sure how to tackle the problem:

for($index = 0; $index < count($_POST['name']; $index++)
 {
  echo 'name: ' . $_POST['name'][$index] . '<br />';
  echo 'meaning: ' . $_POST['meaning'][$index] . '<br /><br />';
 }


first of all u use table instead of ul li

my code is given below, that is running perfectly, u can see here

JAVASCRIPT

<script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    $("#addMore").click(function(){
    $("#nameline").clone().insertAfter("#nameline").slideDown(500);
    $("#meaningline").clone().insertAfter("#nameline").slideDown(500);
    $("#name:last").val("").focus();
    $("#meaning:last").val("");

});
    });
</script>

PHP

<form method="post" action="">
<input type="button" name="addMore" id="addMore" value="add">
<table id="myTable">
    <tbody>
    <tr id="nameline">
        <td><label for="name">Name: </label><input type="text" name="name[]" id="name" /></td>
    </tr>
    <tr id="meaningline">
        <td><label for="meaning">Meaning: </label><input type="text" name="meaning[]" id="meaning" /></td>
    </tr>
    <tr><td><input type="submit" name="submit" value="Save" id="submit" /></td></tr>
    </tbody>
 </table>
</form>
<?php print_r($_POST);?>

for more information u can refere to this link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜