june_c21 Posted April 25, 2008 Share Posted April 25, 2008 i try to run this code but the result is INSERT INTO list (name,age) VALUES ('', '') how to make it insert to the database?? my html code <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Name</title> </head> <body> <form method="POST" action="index2.php"> <!--webbot bot="SaveResults" U-File="C:\wamp\www\_private\form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" --> <table border="1" width="100%"> <tr> <td>Name</td> <td>Age</td> </tr> <tr> <td><input type="text" name="name1" size="20"></td> <td><input type="text" name="age1" size="20"></td> </tr> <tr> <td><input type="text" name="name2" size="20"></td> <td><input type="text" name="age2" size="20"></td> </tr> <tr> <td><input type="text" name="name3" size="20"></td> <td><input type="text" name="age3" size="20"></td> </tr> <tr> <td><input type="text" name="name4" size="20"></td> <td><input type="text" name="age4" size="20"></td> </tr> <tr> <td><input type="text" name="name5" size="20"></td> <td><input type="text" name="age5" size="20"></td> </tr> <tr> <td><input type="text" name="name6" size="20"></td> <td><input type="text" name="age6" size="20"></td> </tr> <tr> <td><input type="text" name="name7" size="20"></td> <td><input type="text" name="age7" size="20"></td> </tr> </table> <p> </p> <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> </body> </html> <?php session_start(); $host = 'localhost'; $user = 'root'; $password = ''; $dbase = 'project'; $dblink = mysql_connect($host,$user,$password); mysql_select_db($dbase,$dblink); $name = $_POST['name']; $age = $_POST['age']; if (strlen('$name')> 0 ){ $query= "INSERT INTO list (name,age) VALUES ('$name', '$age')"; echo $query; $result = mysql_query($query,$dblink); } ?> please help me. i m new with this. thanks Link to comment https://forums.phpfreaks.com/topic/102831-matrik/ Share on other sites More sharing options...
june_c21 Posted April 25, 2008 Author Share Posted April 25, 2008 Please help me with this!!! thanks in advance Link to comment https://forums.phpfreaks.com/topic/102831-matrik/#findComment-526771 Share on other sites More sharing options...
mrdamien Posted April 25, 2008 Share Posted April 25, 2008 Don't bump so quickly. People will help, it just doesn't come instantly. <?php session_start(); $host = 'localhost'; $user = 'root'; $password = ''; $dbase = 'project'; $dblink = mysql_connect($host,$user,$password); mysql_select_db($dbase,$dblink); $query= "INSERT INTO list (name,age) VALUES "; for($i=1; $i<8; $i++){ $name = $_POST['name' . $i]; $age = $_POST['age' . $i]; if(strlen($name) > 0){ $values[] = array('name'=>$name, 'age'=>$age); } } foreach($values as $key=>$value){ $query .= "('{$value['name]}', '{$value['age']}')"; if($key != 7){ $query .= ",\n"; } } echo $query; $result = mysql_query($query,$dblink); ?> (Not tested) Link to comment https://forums.phpfreaks.com/topic/102831-matrik/#findComment-526772 Share on other sites More sharing options...
june_c21 Posted April 25, 2008 Author Share Posted April 25, 2008 i want to stop enter the row into database when user didn't input anything. How to do so? For example, there is 8 rows, if user enter 5 rows. it will store 5 rows in the database only. mrdamien, sorry. but your code didn't work. Link to comment https://forums.phpfreaks.com/topic/102831-matrik/#findComment-526779 Share on other sites More sharing options...
mrdamien Posted April 25, 2008 Share Posted April 25, 2008 Change $name = $_POST['name' . $i]; $age = $_POST['age' . $i]; if(strlen($name) > 0){ $values[] = array('name'=>$name, 'age'=>$age); } to $name = $_POST['name' . $i]; $age = $_POST['age' . $i]; if(strlen($name) > 0 && strlen($age) > 0){ $values[] = array('name'=>$name, 'age'=>$age); } See if that works. Link to comment https://forums.phpfreaks.com/topic/102831-matrik/#findComment-526800 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.