Jump to content

inserting into mysql


Sayre

Recommended Posts

I am getting the error of "query was empty" with this following code I am using. I can't figure it out I have tried everything I could find in every thread I searched.  Can someone please help?

 

<link href="style.css" rel="stylesheet">

<?php
$link=mysql_connect("localhost","user","");
  $database='QA';
  if (!$link)
  die('Failed to connect to Server'.mysql_error());
  $db=mysql_select_db($database, $link);
  session_start();
  if(!$db)
  die('Failed to select Data Base '.mysql_error());
  
  ?>

<table width="700" border="1">
<form id="form1" name="form1" method="post" action="index.php?process">

  <tr>
    <td>Received</td>
    <td><label>
      <input name="received" type="text" id="received" />
    </label></td>
  </tr>
  <tr>
    <td>QA#</td>
    <td>
      <label>
        <input name="qa" type="text" id="qa" />
        </label>
        </td>
  </tr>
  <tr>
    <td>lines</td>
    <td>
      <label>
        <input name="lines" type="text" id="lines" />
        </label>
        </td>
  </tr>
  <tr>
    <td>due </td>
    <td>
      <label>
      <input name="due" type="text" id="due" />
      </label>
        </td>
  </tr>
  <tr>
    <td>
      <label>
      <input type="submit" name="Submit" value="Submit" />
        </label>
    </form>    </td>
  </tr>
</table>

<?php



if(isset($_GET['process']))
{
$sql = "Insert INTO `QA`.`qaData` (received, qa, lines, due,) values('$_POST[received]', '$_POST[qa]','$_POST[lines]','$_POST[due]');";
//echo $query; exit;
$result = mysql_query($query)or die ("Error: ". mysql_error());

}

?>

Link to comment
Share on other sites

Thank you for the reply however I still don't understand the problem, what's wrong with the variables they are called out correctly? Also, I do have error reporting on don't I? Isn't that how I got the error of "query was empty"?  PHP is new to me so I am still learning, I put this script together from what I found on online.

 

 

Link to comment
Share on other sites

The error you're getting is a MySQL error, and is being reported because you explicitly ask for it with mysql_error().

 

Assuming you're developing locally, on a server you have control over, go into your php.ini file, find the error reporting section and edit it so you have the following lines, without leading semi-colons.

error_reporting = -1

display_errors = On

If you can't find the php.ini, or there is more than one, you can see which one php is using by adding phpinfo(); to a script. The value will be under 'Loaded Configuration File'.

 

Then save the file, restart Apache and see what errors you get. These should be your default settings during development, unless you're developing on a live, publicly accessible server. In that case you should log the errors.

 

You can temporarily enable error reporting at runtime on a per-script basis by adding the following to the head of the script. The disadvantage of this method is that fatal parse errors will not be reported because the error stops the execution before the error message can be generated, so you'll end up with a white screen, with no output.

 

error_reporting(-1);
ini_set('display_errors', 'On');

 

Are you sure the variable names are correct? Look again at the variable that holds the query string, and the variable name that is given to execute the query.

Link to comment
Share on other sites

Thank you for the tip on turning on error reporting. I turned it on and the only errors I receive are immediate before the insert script is run;

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Users/Sayre/Sites/qa/index.php:3) in /Users/Sayre/Sites/qa/index.php on line 9

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/Sayre/Sites/qa/index.php:3) in /Users/Sayre/Sites/qa/index.php on line 9

 

However now when I try run the insert script it get this error:

 

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 'lines, due,) values('2012-08-08', '222222','222','08-31-12')' at line 1

 

I am using MAMP btw.  And no I still don't see anything wrong with the variables I am calling out, what do you see wrong?

 

Link to comment
Share on other sites

Your main problem here is the fact that you have output prior to calling session_start.

 

<?php
$link=mysql_connect("localhost","user","");
  $database='QA';
  if (!$link)
  die('Failed to connect to Server'.mysql_error());
  $db=mysql_select_db($database, $link);
  session_start();
  if(!$db)
  die('Failed to select Data Base '.mysql_error());
  
  ?>
<link href="style.css" rel="stylesheet">
<table width="700" border="1">
<form id="form1" name="form1" method="post" action="index.php?process">

  <tr>
    <td>Received</td>
    <td><label>
      <input name="received" type="text" id="received" />
    </label></td>
  </tr>
  <tr>
    <td>QA#</td>
    <td>
      <label>
        <input name="qa" type="text" id="qa" />
        </label>
        </td>
  </tr>
  <tr>
    <td>lines</td>
    <td>
      <label>
        <input name="lines" type="text" id="lines" />
        </label>
        </td>
  </tr>
  <tr>
    <td>due </td>
    <td>
      <label>
      <input name="due" type="text" id="due" />
      </label>
        </td>
  </tr>
  <tr>
    <td>
      <label>
      <input type="submit" name="Submit" value="Submit" />
        </label>
    </form>    </td>
  </tr>
</table>

<?php



if(isset($_GET['process']))
{
$sql = "Insert INTO `QA`.`qaData` (`received`, `qa`, `lines`, `due`) values('$_POST[received]', '$_POST[qa]','$_POST[lines]','$_POST[due]');";
//echo $query; exit;
$result = mysql_query($query)or die ("Error: ". mysql_error());

}

 

Secondly, you had an extra comma in your SQL Statement. Third, I am not sure if lines or qa is a reserved word. I added backticks to combat that. Give it a try and see how it goes.

Link to comment
Share on other sites

Your main problem here is the fact that you have output prior to calling session_start.

 

<?php
$link=mysql_connect("localhost","user","");
  $database='QA';
  if (!$link)
  die('Failed to connect to Server'.mysql_error());
  $db=mysql_select_db($database, $link);
  session_start();
  if(!$db)
  die('Failed to select Data Base '.mysql_error());
  
  ?>
<link href="style.css" rel="stylesheet">
<table width="700" border="1">
<form id="form1" name="form1" method="post" action="index.php?process">

  <tr>
    <td>Received</td>
    <td><label>
      <input name="received" type="text" id="received" />
    </label></td>
  </tr>
  <tr>
    <td>QA#</td>
    <td>
      <label>
        <input name="qa" type="text" id="qa" />
        </label>
        </td>
  </tr>
  <tr>
    <td>lines</td>
    <td>
      <label>
        <input name="lines" type="text" id="lines" />
        </label>
        </td>
  </tr>
  <tr>
    <td>due </td>
    <td>
      <label>
      <input name="due" type="text" id="due" />
      </label>
        </td>
  </tr>
  <tr>
    <td>
      <label>
      <input type="submit" name="Submit" value="Submit" />
        </label>
    </form>    </td>
  </tr>
</table>

<?php



if(isset($_GET['process']))
{
$sql = "Insert INTO `QA`.`qaData` (`received`, `qa`, `lines`, `due`) values('$_POST[received]', '$_POST[qa]','$_POST[lines]','$_POST[due]');";
//echo $query; exit;
$result = mysql_query($query)or die ("Error: ". mysql_error());

}

 

Secondly, you had an extra comma in your SQL Statement. Third, I am not sure if lines or qa is a reserved word. I added backticks to combat that. Give it a try and see how it goes.

 

 

THANK YOU! that was it, I guess they are reserved words. I never would have thought of that.

Link to comment
Share on other sites

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.