Trying to insert into database

I get the following message:

name4Query failed: Unknown column ‘late’ in ‘field list’

with this code. What does it mean? I know it found the 4th record.

<?php
$apt=$_POST['search_term'];
$stat = mysql_connect("localhost","root","");
$stat = mysql_select_db("prerentdb");
$query = "SELECT name FROM payments WHERE late = 'L'";
$stat = @mysql_fetch_assoc(mysql_query($query));
echo $stat["name"];
$name=$_POST['name'];  
$apt=$_POST['apt']; 
$amtpaid=$_POST['amtpaid'];  
$rentdue=$_POST['rentdue'];  
$prevbal=$_POST['prevbal'];
$hudpay=$_POST['hudpay'];  
$tentpay=$_POST['tentpay']; 
$datepaid=$_POST['datepaid'];
$late=$_POST['late']; 
$comments=$_POST['comments'];  
$paidsum=$_POST['paidsum'];
$query = "
INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,
hudpay,tentpay,datepaid,late,comments,paidsum)
VALUES('$name','$apt','$amtpaid','$rentdue','$prevbal',
'$hudpay','$tentpay','$datepaid','$late','$comments','$paidsum')";
$stat = mysql_query($query) or die('Query failed: ' . mysql_error());  
mysql_close();
echo "data inserted<br /><br />"; 
?>

the error message is telling you that the table mentioned in the query does not have a column called late

by the way, you don’t have to pull data out of a table using php if all you want to do is turn around and insert that data into another table – you can do it all with a single INSERT SELECT statement

edit: scratch that, i think you’re getting the values from a form (i don’t actually do php)

so which of the two queries is generating the error? and why are you extracting the name in the first query?

Below is my code and below that, the payments table with the late(L). An insertion is made into the payhist table but without name, apt, datepaid late, comments or paidsum, only zeros for the other fields. I get the echo

“data inserted”
?

<?php
mysql_connect(localhost,root,"");
mysql_select_db(prerentdb) or die( "Unable to select database");
$query="SELECT * FROM payments Where late='L'";
$result=mysql_query($query);
$num=mysql_numrows($result);
$name=$_POST['name'];  
$apt=$_POST['apt']; 
$amtpaid=$_POST['amtpaid'];  
$rentdue=$_POST['rentdue'];  
$prevbal=$_POST['prevbal'];
$hudpay=$_POST['hudpay'];  
$tentpay=$_POST['tentpay']; 
$datepaid=$_POST['datepaid'];
$late=$_POST['late']; 
$comments=$_POST['comments'];  
$paidsum=$_POST['paidsum'];
$query = "
INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,
hudpay,tentpay,datepaid,late,comments,paidsum)
VALUES('$name','$apt','$amtpaid','$rentdue','$prevbal',
'$hudpay','$tentpay','$datepaid','$late','$comments','$paidsum')";
echo "data inserted</font><br /><br />"; 
$stat = mysql_query($query) or die('Query failed: ' . mysql_error());  
mysql_close();
?>

Payment Report… for January / 2011
Apt Rent Prev Late Sec . Court . Tenant Hud Date .
Tenant # Paid Due Bal Chg Dep Dmg Cost NSF Pay Paid L Comment

name1 k1 0 500 0 0 0 0 0 0 0 0
name2 k2 300 500 10 10 0 35 0 0 300 0 1/19/2011 part pay
name3 k3 550 550 0 0 0 0 0 0 550 0 1/05/2011
name4 k4 510 500 0 0 0 0 0 0 510 0 1/09/2011L

so now it’s working? you didn’t get “Unknown column ‘late’ in ‘field list’” error?

no errors, it just inserts zeros in the integer fields(see previous post

Do a print_r($_POST); to check the values sent to the script.
And pass them through mysql_real_escape_string() before using them in the query to prevent sql injection.

Also for debugging purposes instead of echoing ‘data inserted’ BEFORE you execute the insert query, you might want to echo that after, and echo the value of $query as well.

can you give me an example of how to do that?


<?php
// check the $_POST array for debugging purposes
echo "$_POST: "; print_r($_POST); echo "<br />";

mysql_connect(localhost,root,"");
mysql_select_db(prerentdb) or die( "Unable to select database");
$query="SELECT * FROM payments Where late='L'";
$result=mysql_query($query);
$num=mysql_numrows($result);
$name=$_POST['name'];  
$apt=$_POST['apt']; 
$amtpaid=$_POST['amtpaid'];  
$rentdue=$_POST['rentdue'];  
$prevbal=$_POST['prevbal'];
$hudpay=$_POST['hudpay'];  
$tentpay=$_POST['tentpay']; 
$datepaid=$_POST['datepaid'];
$late=$_POST['late']; 
$comments=$_POST['comments'];  
$paidsum=$_POST['paidsum'];
$query = "
INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,
hudpay,tentpay,datepaid,late,comments,paidsum)
VALUES(
    '" . mysql_real_escape_string($name) . "'
  , '" . mysql_real_escape_string($apt) . "'
  , '" . mysql_real_escape_string($amtpaid) . "'
  , '" . mysql_real_escape_string($rentdue) . "'
  , '" . mysql_real_escape_string($prevbal) . "'
  , '" . mysql_real_escape_string($hudpay) . "'
  , '" . mysql_real_escape_string($tentpay) . "'
  , '" . mysql_real_escape_string($datepaid) . "'
  , '" . mysql_real_escape_string($late) . "'
  , '" . mysql_real_escape_string($comments) . "'
  , '" . mysql_real_escape_string($paidsum) . "'
)";
$stat = mysql_query($query) or die('Query failed: ' . mysql_error() . " - query: $query");  
echo "data inserted</font><br /><br />"; 
mysql_close();

// echo $query for debugging purposes
echo "query: $query<br />";
?>

The below is what I got:

Array: Array ( )
data inserted

query: INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal, hudpay,tentpay,datepaid,late,comments,paidsum) VALUES( ‘’ , ‘’ , ‘’ , ‘’ , ‘’ , ‘’ , ‘’ , ‘’ , ‘’ , ‘’ , ‘’ )

the record with the late(L) is there in the payments table as well as all the other fields(see previous post)

But you aren’t inserting in the payments table, you’re inserting in the payhist table. And you’re inserting with all empty values. Because the values you’re getting from the $_POST super global don’t exist. $_POST is empty.
Where exactly are you getting the to be inserted values from?

ok, so you see how litle I know. I’m trying to get the values from the Payments table and thought I was doing just that

if you want to insert payment data into payhist, i mentioned in post #2 the INSERT SELECT sttement

I’ve been studying this all day and came up with the below which doesn’t work?

<?php
// check the $_POST array for debugging purposes
echo "$_POST: "; print_r($_POST); echo "<br />";
mysql_connect(localhost,root,"");
mysql_select_db(prerentdb) or die( "Unable to select database");
$result=mysql_query($query);
$num=mysql_numrows($result);
$query = "
INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,
hudpay,tentpay,datepaid,late,comments,paidsum)
Select 11,
name,apt,amtpaid,rentdue,prevbal,hudpay,tentpay,datepaid,late,comments,paidsum
From payments
WHERE late = 'L';
$stat = mysql_query($query) or die('Query failed: ' . mysql_error() . " - query: $query");  
echo "data inserted</font><br /><br />"; 
mysql_close();
// echo $query for debugging purposes
echo "query: $query<br />";
?>

i’m sorry, “doesn’t work” is not a valid mysql error message :slight_smile:

did you test your query outside of php first?

INSERT 
  INTO payhist 
     ( name
     , apt
     , amtpaid
     , rentdue
     , prevbal
     , hudpay
     , tentpay
     , datepaid
     , late
     , comments
     , paidsum ) 
SELECT 11
     , name
     , apt
     , amtpaid
     , rentdue
     , prevbal
     , hudpay
     , tentpay
     , datepaid
     , late
     , comments
     , paidsum 
  FROM payments 
 WHERE late = 'L' 

do me a favour and count the number of columns in the INTO list, and then count the number of columns in the SELECT list

It may be a copy & paste error, but as you can see from the colours in the code you posted, you’re missing the closing quotes (") at the end of the query.

and once that’s fixed, the query itself has an error :wink:

One thing at a time :smiley:

I really appreciate your help. I removed the “11” from the select list and got the below error message:

Parse error: syntax error, unexpected ‘:’ in C:\xampp\htdocs\hofiles\lateinsert.php on line 14
INSERT
INTO payhist
( name
, apt
, amtpaid
, rentdue
, prevbal
, hudpay
, tentpay
, datepaid
, late
, comments
, paidsum )
SELECT name
, apt
, amtpaid
, rentdue
, prevbal
, hudpay
, tentpay
, datepaid
, late
, comments
, paidsum
FROM payments
WHERE late = ‘L’

Let me quote myself :slight_smile:

Thanks for the help guys. ok, this is what I have and the message:

<?php
// check the $_POST array for debugging purposes
echo "$_POST: "; print_r($_POST); echo "<br />";
mysql_connect(localhost,root,"");
mysql_select_db(prerentdb) or die( "Unable to select database");
$result=mysql_query($query);
$num=mysql_numrows($result);
$query = "
INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,
hudpay,tentpay,datepaid,late,comments,paidsum)"
Select name,apt,amtpaid,rentdue,prevbal,hudpay,tentpay,datepaid,late,comments,paidsum
From payments
WHERE paidsum= rentdue OR late = 'L';
$stat = mysql_query($query) or die('Query failed: ' . mysql_error() . " - query: $query");  
echo "data inserted</font><br /><br />"; 
mysql_close();
// echo $query for debugging purposes
echo "query: $query<br />";
?>

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\hofiles\lateinsert.php on line 11