We help IT Professionals succeed at work.

Problems with PHP/MySQL code

Robert Thomas
Last Modified: 2013-12-12
I keep getting an error with the following php MySQL code.  It keeps given me an error around line 1.  Line one is the connection to the database created by dreamweaver, so Im not sure why it would be giving me an error. Here is the code:

<?php require_once('Connections/prism.php'); ?>
mysql_select_db($database_prism, $prism);
$query_Recordset1 = "SELECT * FROM `user`";
$Recordset1 = mysql_query($query_Recordset1, $prism) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>


 // Where the file is going to be placed
$target_path = "webquest/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded
       echo $location;
} else{
    echo "There was an error uploading the file, please try again!";

$name = $_POST['name'];
$description = $_POST['description'];
$subject = $_POST['subject'];
$grade = $_POST['grade'];
$author = $_POST['author'];
$location = "/". $target_path . "/";
$ID = $row_Recordset1['user_id'];

$sql = mysql_query("INSERT INTO webquest (author, description, grade, name, subject, post_date, user_Id) VALUES ('$author', '$description', '$grade', '$name', '$subject', now(), '$ID'") or die(mysql_error());

      echo 'There has been an error uploading your file. Please contact the webmaster.';
}else {
      echo 'Your file has been succefully uploaded. :)';

Watch Question

Are you able to provide the full text of the error?

Does the folder 'Connections' exist in the current directory, and does it contain a readable file named 'prism.php'?
Robert ThomasProgram Analyst / Web Developer


Yes. Connections does exist. Here is the full error:

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 '' at line 1

Try removing the quotes from your table name:

"SELECT * from user;"
AlexanderREnterprise Web Developer

Can you please post Connections/prism.php  ?
What is your PHP MySQL version?

Also i would be carefull with using single quotes and double quotes together in  VALUES ('$author', '$description', '$grade', '$name', '$subject', now(), '$ID'")  My personal preference is to use single quotes every where, and concatinate $vars to it.  It should not (i think) make a difference in your code though.
Top Expert 2004

>>> Here is the full error:

Are you sure that's the full error?  Usually, PHP prints out a filename and the PHP line number where the error occured.  This is important information, since it tells us if prism.php is successful or not.  I think the connections file is running ok, but we can't tell for sure without knowing which line is generating the error.

One thing I did notice is your use of the now() function inside a query.  Change your insert query to look like this:

$query = "INSERT INTO webquest (author,description,grade,name,subject,post_date,user_Id) VALUES ('$author','$description','$grade','$name','$subject'," . time() . ",'$ID'";
if (!($result = mysql_query($query))) {
  //  handle the SQL error
In the line
$Recordset1 = mysql_query($query_Recordset1, $prism) or die(mysql_error());

what is the value of $query_Recordset1?
This one is on us!
(Get your first solution completely free - no credit card required)

try changing:

$sql = mysql_query("INSERT INTO webquest (author, description, grade, name, subject, post_date, user_Id) VALUES ('$author', '$description', '$grade', '$name', '$subject', now(), '$ID'") or die(mysql_error());


$sql = mysql_query("INSERT INTO webquest (`author`,` description`,` grade`,` name`,` subject`,` post_date`,` user_Id`) VALUES ('$author', '$description', '$grade', '$name', '$subject', now(), '$ID'") or die(mysql_error());

Sometimes column names are also functions for mysql, so putting them in tick marks will solve that.  Doesn't look like the case, but it may work.  

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.