Jump to content

Recommended Posts

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));
				}
				
				
			}
Link to comment
https://forums.phpfreaks.com/topic/288828-insert-function-not-working/
Share on other sites

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

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>';

 

$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 by ginerjm
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.