pcristian43 Posted May 28, 2014 Share Posted May 28, 2014 Hello all, so I created an insert function and it seems no matter what I try that it won't add values using the query function inside a table from the respective variables, I would like to know why is this happening? Here is the code can you tell me why it doesn't insert anything in the database? It shows no errors when it runs but then again when I check the tables they're empty! function insert(){ $user = $_POST['user']; $pass = md5($_POST['pass']); $priv = "User"; $mail = $_POST['mail']; $avatar = $_FILES['avatar']['name']; $date="now()"; $submit = $_POST['submit']; $query = "INSERT INTO user(user,pass,priv,mail,avatar,date) VALUES(`$user`,`$pass`,`$priv`,`$mail`,`$avatar`,`$date`);"; if($submit){ $res = mysqli_query($con,$query) or die(mysqli_error($con)); } } Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/ Share on other sites More sharing options...
PravinS Posted May 28, 2014 Share Posted May 28, 2014 remove the back ticks (``) from VALUES of INSERT query and use single quotes (' ') $query = "INSERT INTO user(user,pass,priv,mail,avatar,date) VALUES('$user','$pass','$priv','$mail','$avatar','$date')"; may this works for you Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481129 Share on other sites More sharing options...
pcristian43 Posted May 28, 2014 Author Share Posted May 28, 2014 remove the back ticks (``) from VALUES of INSERT query and use single quotes (' ') $query = "INSERT INTO user(user,pass,priv,mail,avatar,date) VALUES('$user','$pass','$priv','$mail','$avatar','$date')"; may this works for you I tried that already, even escaping strings, it still shows nothing inside the table. The only thing that executes is the CREATE TABLE statement Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481131 Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 Have you tried adding some debugging statements to see if the query is even being executed? You could do something like this, for example: <?php if($submit){ echo '<div>Executing Query</div>'; $res = mysqli_query($con,$query) or die(mysqli_error($con)); } else { echo '<div>Query Skipped</div>'; } ?> If the "Query Skipped" message is displayed, try echoing the contents of the $_POST array to see if it contains a value for "submit". echo '<pre>' . print_r($_POST, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481155 Share on other sites More sharing options...
Barand Posted May 28, 2014 Share Posted May 28, 2014 now() shouldn't have quotes round it in the query Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481190 Share on other sites More sharing options...
pcristian43 Posted May 28, 2014 Author Share Posted May 28, 2014 now() shouldn't have quotes round it in the query If it doesn't its seen as a php function instead of SQL one, that's why I placed the quotes. Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481193 Share on other sites More sharing options...
pcristian43 Posted May 28, 2014 Author Share Posted May 28, 2014 The script also shows me this error message: Warning: mysqli_query() expects parameter 1 to be mysqli, null given in on line 50. Line 50 is the $res variable. Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481194 Share on other sites More sharing options...
ginerjm Posted May 28, 2014 Share Posted May 28, 2014 (edited) $con is not in scope inside your function. Either make it global or pass it in as a function parameter And - I think you want to remove the single quotes from the value $date. And - you probably need backticks on the field name date since date is probably a reserved word Edited May 28, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481201 Share on other sites More sharing options...
Ch0cu3r Posted May 29, 2014 Share Posted May 29, 2014 Correction to ginerjm post $con is not in scope inside your function. Either make it global or pass it in as a function parameter Read the manual on how to use function arguments Quote Link to comment https://forums.phpfreaks.com/topic/288828-insert-function-not-working/#findComment-1481252 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.