help w/code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ckdoublenecks
Forum Newbie
Posts: 21
Joined: Mon Jan 24, 2011 6:47 pm

help w/code

Post 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();
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: help w/code

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help w/code

Post by Christopher »

Oh ... and lining up the code on the semi-colon causes madness! ;)
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help w/code

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: help w/code

Post 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 />";
}
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help w/code

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: help w/code

Post 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.
Post Reply