mysql - PHP query keeps failing -
<html> <head> <title>inserts</title> </head> <body> <h1>adding user</h1> <form action="insert1.php" method="post"> gamertag:<br /> <input name="insertfield1" type="text"> <br /> bio:<br /> <input name="insertfield2" type="text"> <br /> membership:<br /> <input name="insertfield3" type="text"> <br /> gamerscore:<br /> <input name="insertfield4" type="number"> <br /> email:<br /> <input name="insertfield5" type="text"> <br /> name:<br /> <input name="insertfield6" type="text"> <br /> location:<br /> <input name="insertfield7" type="text"> <br /> friend:<br /> <input name="insertfield8" type="text"> <br /> <input type="submit" value="insert"> </form> </body> </html>
end html begin php
</head> <body> <h1>insert test results</h1> <?php // create short variable names $insertfield1=$_post["insertfield1"]; $insertfield2=$_post["insertfield2"]; $insertfield3=$_post["insertfield3"]; $insertfield4=$_post["insertfield4"]; $insertfield5=$_post["insertfield5"]; $insertfield6=$_post["insertfield6"]; $insertfield7=$_post["insertfield7"]; $insertfield8=$_post["insertfield8"]; //use trim function strip whitespace inadvertently entered @ beginning or end of insert term $insertfield1= trim($insertfield1); $insertfield2= trim($insertfield2); $insertfield3= trim($insertfield3); $insertfield4= trim($insertfield4); $insertfield5= trim($insertfield5); $insertfield6= trim($insertfield6); $insertfield7= trim($insertfield7); $insertfield8= trim($insertfield8); echo "first insert term entered: $insertfield1"; echo "<br>"; echo "second insert term entered: $insertfield2"; echo "<br>"; echo "third insert term entered: $insertfield3"; echo "<br>"; echo "fourth insert term entered: $insertfield4"; echo "<br>"; echo "fifth insert term entered: $insertfield5"; echo "<br>"; echo "sixth insert term entered: $insertfield6"; echo "<br>"; echo "seventh insert term entered: $insertfield7"; echo "<br>"; echo "eight insert term entered: $insertfield8"; echo "<br>"; if (!$insertfield1) { echo 'you have not entered insert details. please go , try again.'; exit; } // connect database $link=mysqli_connect("yourmysqlserver", "usernamehere", "mypassword", "mydatabase") or die('could not connect '); echo "connected <br>"; // insert new data table $result = mysqli_query($link,"insert userprofile values ('$insertfield1', '$insertfield2', '$insertfield3', $insertfield4, '$insertfield5', '$insertfield6', '$insertfield7', '$insertfield8' ) " ) or die("query failed "); echo "query ok\n"; $num_results = mysqli_num_rows($result); echo '<p>number of rows inserted in userprofile: '.$num_results.'</p>'; // query table show new data inserted $result = mysqli_query($link,"select * userprofile") or die("query failed "); echo "query ok\n"; $num_results = mysqli_num_rows($result); echo '<p>new number of rows in userprofile: '.$num_results.'</p>'; // show results of table new data inserted echo " <table border='1'>\n"; while ($line = mysqli_fetch_array($result, mysql_assoc)) { echo "\t<tr>\n"; foreach ($line $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; //free result set mysqli_free_result($result); //close connection mysqli_close($link); ?> </body> </html>
for reason these aren't communicating each other correctly. assumption there wrong in php code. can tell can connect correctly when try insert doesn't , says query failed.
1) try using format,
mysqli_query($link,"insert userprofile (col1,col2,col3) values ('value1','value2','value3')");
and make sure don't miss anything.
2) try changing value gamescore varchar, , make changes while inserting too.
3) check permitted value length in every table columns, make sure input not exceeding permitted length