-2
$dbc=mysql_connect('127.0.0.1', 'root', '1234','aliendatabase')
or die('Failed!');

$query = "INSERT INTO alien_abduction(first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', 
'$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', 
'$email')"; 

$result=mysql_query($query)
or die("Failed to upload!!!!");

mysql_close($dbc);

This code is unable to execute $result line (so outputs Failed to upload!!!!) but it is able to establish connection. I have cross-checked the table column name and variables and it seems fine.

MySQL version 5.7

5
  • 1
    WARNING: If you're just learning PHP, please, do not use the mysql_query interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way explains best practices. Your user data is not properly escaped and there are SQL injection bugs and can be exploited. Commented Dec 21, 2017 at 19:22
  • 2
    If only there were a way to get more info about the nature of the error. Commented Dec 21, 2017 at 19:25
  • Okay, I will learn it but can I know why this code doesn't run? I was referring to a book called Head First PHP & MySQL. Commented Dec 21, 2017 at 19:30
  • A book? Wait, what is a BOOK?!?! ... :D Switch all your mysql_* to mysqli_* at the very least. Commented Dec 21, 2017 at 19:39
  • @IncredibleHat It doesn't work. I have tried it. I didn't even connect to the server. Commented Dec 21, 2017 at 19:41

2 Answers 2

0

Please check the Datatypes of the columns and do the following to find the error:

$result=mysql_query($query) or die(mysql_error($dbc));

Sign up to request clarification or add additional context in comments.

6 Comments

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , , , , , , no, , )' at line 1 Which line 1 is it referring to?
Aman, can you please do an echo of the SQL and paste it here
echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: '.$how_many.'<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: '.$what_they_did."<br />"; echo 'Was Fang there? ' . $fang_spotted . '<br />'; (Error is in this line as I figured out.) echo 'Other Comments: '.$other.'<br />'; echo 'Your email address is ' . $email;
I hope it is readable or I can upload an image.
try: $query = "INSERT INTO alien_abduction(first_name, last_name, when_it_happened, how_long, " . "how_many, alien_description, what_they_did, fang_spotted, other, email) " . "VALUES ('".stripslashes($first_name)."', '".stripslashes($last_name)."', '".stripslashes($when_it_happened)."', '".stripslashes($how_long)."', '".stripslashes($how_many)."', '".stripslashes($alien_description)."', '".stripslashes($what_they_did)."', '".stripslashes($fang_spotted)."', '".stripslashes($other)."', '".stripslashes($email)."')";
|
-2

The problem is with your quoting way. As single quote won't be used to hold variables. So in single quote, they are simply strings, not the variables.

If you want that $variable replaced with value then rewrite the whole query in single quote like below

$query = 'INSERT INTO alien_abduction(first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email) '.
 ' VALUES (' ".$first_name." ', ' ".$last_name . " ', ' ". $when_it_happened . " ', ' " .$how_long . " ', ' " . $how_many . " ', " . " " . $alien_description . " ', ' " . $what_they_did . " ', ' " . $fang_spotted . " ', ' " . $other . " ', ' " . $email . " ')' ; 

I hope that will solve the issue.

5 Comments

We can use double quotes everywhere. It isn't a problem as it recognizes the variable too.
Problem is single quote not the double quotes
I don't understand why Er. Amit Joshi and I got -1 for actually solving your problem.
The string literal saved into $query uses double quotes, so the variables are parsed. The fact that these variables are also surrounded by single quotes inside the double quotes is irrelevant because it is SQL-level quoting, not PHP level. On top of that, even if unparsed variables was the issue, it would simply insert the unparsed variable names into the database without raising an error.
Kindly look into this post stackoverflow.com/questions/3446216/… thanks bro for @Benjamin Racette we are not wrong at all. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.