shashidharkulkarni Posted November 10, 2015 Share Posted November 10, 2015 i am trying to put form data to mysql table what's wrong with my code below does not give any error but not inserting any data in table <html> <head> <title>HTML formS</title> </head> <body> <form action="insertform.php" method="post"> Name : <input type="text" name="name"><br> address : <input type="text" name="address"><br> topic :<input type="text" name="topic1"><br> <input type="submit" value="submit"> </form> <?php $name = $_POST['name']; $address = $_POST['address']; $topic1 = $_POST['topic1']; if (isset($_POST['submit'])){ $con = mysql_connect("localhost","db","password"); if (!$con){ die("can not connect".mysql_error()); } mysql_select_db("db",$con); $sql= "INSERT INTO student_register_test10 (name, address, topic1) values ($name, $address, $topic1)"; mysql_query($sql,$con); if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "Success!"; mysql_close($con); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/299424-pls-am-a-beginer/ Share on other sites More sharing options...
benanamen Posted November 10, 2015 Share Posted November 10, 2015 (edited) You have several issues. First, you are using deprecated code. You need to use PDO with prepared statements. Next, you are setting post variables outside of your isset post check so you have errors there. If you turned on error reporting you would see that as well as any other errors You are also mixing mysql with mysqli. You cant do that. Your code is also vulnerable to SQL Injection. You NEVER EVER send user supplied data directly to the database. You need to rewrite the whole thing. None of your code is any good. Edited November 10, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/299424-pls-am-a-beginer/#findComment-1526143 Share on other sites More sharing options...
Barand Posted November 10, 2015 Share Posted November 10, 2015 In addition, your SQL query syntax is wrong. String values need to be in single quotes INSERT INTO student_register_test10 (name, address, topic1) values ('$name', '$address', '$topic1' ) Quote Link to comment https://forums.phpfreaks.com/topic/299424-pls-am-a-beginer/#findComment-1526146 Share on other sites More sharing options...
ginerjm Posted November 11, 2015 Share Posted November 11, 2015 And you attempt to access something named 'submit' in the POST array. You didn't assign a name= to your submit button so you have none. For future reference (your next post?) - it's a better idea to actually indicate what you want in the subject/topic you post under. Naming your post as you did here is not much help in getting the attention you seek, other than from the curious who have the time to browse everything. Better pinpoint a topic that you wish help on than hope for someone's curiosity to cause them to read your question. Quote Link to comment https://forums.phpfreaks.com/topic/299424-pls-am-a-beginer/#findComment-1526195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.