Page 1 of 1

help w/code

Posted: Thu Mar 07, 2013 11:25 am
by ckdoublenecks
will someone explain what's wrong ? I just get "query failed"

Code: Select all

<?php
    $status=$_POST['status'];
            $pd=$_POST['pd'];
    $payrec=$_POST['payrec'];
    $acctno=$_POST['acctno'];
  $orderno=$_POST['orderno'];
      $bname=$_POST['bname'];      
  $bstreet=$_POST['bstreet'];    
      $bcity=$_POST['bcity'];
    $bstate=$_POST['bstate'];
        $bzip=$_POST['bzip'];
    $bemail=$_POST['bemail'];    
      $phone=$_POST['phone']; 
  $contact=$_POST['contact'];
      $sname=$_POST['sname'];  
  $sstreet=$_POST['sstreet'];
      $scity=$_POST['scity'];
    $sstate=$_POST['sstate'];
        $szip=$_POST['szip'];
    $semail=$_POST['semail'];
      $terms=$_POST['terms'];
          $fob=$_POST['fob'];
$shipdate=$_POST['shipdate'];
  $shipamt=$_POST['shipamt'];  
  $dateord=$_POST['dateord'];
$datecomp=$_POST['datecomp'];
  $duedate=$_POST['duedate']; 
$datepaid=$_POST['datepaid'];  
          $qty=$_POST['qty'];  
      $descr=$_POST['descr'];  
  $charges=$_POST['charges'];
          $tax=$_POST['tax'];  
  $paidamt=$_POST['paidamt'];
$dayslate=$_POST['dayslate'];
  $checkno=$_POST['checkno'];        
    $amtdue=$_POST['amtdue'];

$stat = mysql_connect('localhost','root',' ') or die('Unable to connect to database: ' . mysql_error()); 
$stat = mysql_select_db('oodb') or die('Unable to select database: ' . mysql_error()); 
$query = "INSERT INTO oocust VALUES('$status','$pd','$payrec','$acctno','$orderno','$bname','$bstreet','$bcity','$bstate',
'$bzip','$bemail','$phone','$contact','$sname','$sstreet','$scity','$sstate','$szip','$semail','$terms','$fob','$shipdate','$shipamt',
'$dateord','$datecomp','$duedate','$datepaid','$qty','$descr','$charges','$tax','$paidamt','$dayslate','$checkno','$amtdue')";
echo "data inserted</font><br /><br />";
    
	$stat = mysql_query($query) or die('Query failed: ' . mysql_error()); 

	mysql_close();
?>

Re: help w/code

Posted: Thu Mar 07, 2013 11:44 am
by social_experiment
An INSERT query has the following format

Code: Select all

INSERT INTO `table` (`field1`, `field2`) VALUES ('value1', 'value2')
Your query doesn't have the fields; the amount of fields should be matched by the amount of values passed to the query

Re: help w/code

Posted: Thu Mar 07, 2013 12:32 pm
by Christopher
Oh ... and lining up the code on the semi-colon causes madness! ;)

Re: help w/code

Posted: Thu Mar 07, 2013 12:48 pm
by requinix
social_experiment wrote:An INSERT query has the following format

Code: Select all

INSERT INTO `table` (`field1`, `field2`) VALUES ('value1', 'value2')
Your query doesn't have the fields; the amount of fields should be matched by the amount of values passed to the query
The fields list is optional if you're inserting a value for every single column.

What is the exact value of $query?

Re: help w/code

Posted: Fri Mar 08, 2013 6:39 am
by social_experiment
requinix wrote:The fields list is optional if you're inserting a value for every single column.
this i didn't know :) could also explain why OP doesn't paste a mysql related error

on a different matter; if you want to echo that the data was inserted after it was actually written to the database you should look at using a conditional statement;

Code: Select all

if ($stat) {
 echo "data inserted</font><br /><br />";
}

Re: help w/code

Posted: Fri Mar 08, 2013 12:50 pm
by requinix
social_experiment wrote:this i didn't know :) could also explain why OP doesn't paste a mysql related error
It's rarely used in practice: you have to include a value for the auto-incrementing primary key (NULL lets it generate one as usual), you have to know the order of fields, isn't self-documenting like an INSERT with column names or an INSERT... SET, and will instantly break if a new field is added.

Re: help w/code

Posted: Fri Mar 08, 2013 3:59 pm
by califdon
social_experiment wrote:What is the exact value of $query?
What he is suggesting is that you echo the variable $query right after you populate it, so that you can examine the mysql query as it is actually sent to the database engine. This is the very first thing you should do when debugging a MySQL error; often it will immediately become clear what is wrong.