Jump to content

trying to insert into database

- - - - -

  • Please log in to reply
8 replies to this topic

#1
ckdoublenecks

ckdoublenecks

    Newbie

  • Members
  • Pip
  • 5 posts
I used your below code and got the message below. it entered the integer fields but not the name, apt, datepaid, comments. I'm trying to read one database table(payments) and insert records from that one into the next (payhist)

<?php

mysql_connect(localhost,root,"");

mysql_select_db(prerentdb) or die( "Unable to select database");

$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')";

$query2 ="Select *From payments WHERE late = 'L'";

mysql_query($query);

mysql_query($query2) 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 />";

?>

Quote

data inserted

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


#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Where are the variables being set at?

<?PHP
$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)')"; 


#3
ckdoublenecks

ckdoublenecks

    Newbie

  • Members
  • Pip
  • 5 posts
this is the latest. it does nothing

<?php

mysql_connect(localhost,root,"");

mysql_select_db(prerentdb) or die( "Unable to select database");

$name = mysql_real_escape_string($_POST['name']);

$apt = mysql_real_escape_string($_POST['apt']);

$amtpaid = mysql_real_escape_string($_POST['amtpaid']);

$rentdue = mysql_real_escape_string($_POST['rentdue']);

$prevbal = mysql_real_escape_string($_POST['prevbal']);

$hudpay = mysql_real_escape_string($_POST['hudpay']);

$tentpay = mysql_real_escape_string($_POST['tentpay']);

$datepaid = mysql_real_escape_string($_POST['datepaid']);

$comments = mysql_real_escape_string($_POST['comments']);

$paidsum = mysql_real_escape_string($_POST['paidsum']);

$name = $_POST['name'];$apt = $_POST['apt'];

$amtpaid = $_POST['amtpaid'];

$rentdue = $_POST['rentdue'];

$prevbal = $_POST['prevbal'];

$hudpay = $_POST['hudpay'];

$tentpay = $_POST['tentpay'];

$datepaid = $_POST['datepaid'];

$comments = $_POST['comments'];

$paidsum = $_POST['paidsum'];

$query = "INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,hudpay,tentpay,datepaid,comments,paidsum) 

VALUES ('$name', '$apt', '$amtpaid', '$rentdue', '$prevbal', '$hudpay', '$tentpay', '$datepaid', '$comments', '$paidsum')";

$query2 ="Select * From payments WHERE late = 'L'";

mysql_query($query);

mysql_query($query2) 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 />";

?>


#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Are you getting the correct variables?
Try this:
<?PHP
print_r($_POST);

Also, why are you setting the variables twice? The second time you set them you void the escaping you did originally since you resetting directly from the post.

<?PHP
$name = mysql_real_escape_string($_POST['name']);
$apt = mysql_real_escape_string($_POST['apt']);
$amtpaid = mysql_real_escape_string($_POST['amtpaid']);
$rentdue = mysql_real_escape_string($_POST['rentdue']);
$prevbal = mysql_real_escape_string($_POST['prevbal']);
$hudpay = mysql_real_escape_string($_POST['hudpay']);
$tentpay = mysql_real_escape_string($_POST['tentpay']);
$datepaid = mysql_real_escape_string($_POST['datepaid']);
$comments = mysql_real_escape_string($_POST['comments']);
$paidsum = mysql_real_escape_string($_POST['paidsum']);


#5
ckdoublenecks

ckdoublenecks

    Newbie

  • Members
  • Pip
  • 5 posts
this is the actual program and results
<?php

error_reporting(E_ALL); 

mysql_connect(localhost,root,"");

mysql_select_db(prerentdb) or die( "Unable to select database");

$name = mysql_real_escape_string($_POST['name']);

$apt = mysql_real_escape_string($_POST['apt']);

$amtpaid = mysql_real_escape_string($_POST['amtpaid']);

$rentdue = mysql_real_escape_string($_POST['rentdue']);

$prevbal = mysql_real_escape_string($_POST['prevbal']);

$hudpay = mysql_real_escape_string($_POST['hudpay']);

$tentpay = mysql_real_escape_string($_POST['tentpay']);

$datepaid = mysql_real_escape_string($_POST['datepaid']);

$comments = mysql_real_escape_string($_POST['comments']);

$paidsum = mysql_real_escape_string($_POST['paidsum']);

$name = $_POST['name'];$apt = $_POST['apt'];

$amtpaid = $_POST['amtpaid'];

$rentdue = $_POST['rentdue'];

$prevbal = $_POST['prevbal'];

$hudpay = $_POST['hudpay'];

$tentpay = $_POST['tentpay'];

$datepaid = $_POST['datepaid'];

$comments = $_POST['comments'];

$paidsum = $_POST['paidsum'];

$query = "INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,hudpay,tentpay,datepaid,comments,paidsum) 

VALUES ('$name', '$apt', '$amtpaid', '$rentdue', '$prevbal', '$hudpay', '$tentpay', '$datepaid', '$comments', '$paidsum')";

$query2 ="Select * From payments WHERE late = 'L'";

mysql_query($query);

mysql_query($query2) 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 />";

?>

Quote

Notice: Use of undefined constant localhost - assumed 'localhost' in C:\xampp\htdocs\hofiles\lateinsert.php on line 3
Notice: Use of undefined constant root - assumed 'root' in C:\xampp\htdocs\hofiles\lateinsert.php on line 3
Notice: Use of undefined constant prerentdb - assumed 'prerentdb' in C:\xampp\htdocs\hofiles\lateinsert.php on line 4
Notice: Undefined index: name in C:\xampp\htdocs\hofiles\lateinsert.php on line 5
Notice: Undefined index: apt in C:\xampp\htdocs\hofiles\lateinsert.php on line 6
Notice: Undefined index: amtpaid in C:\xampp\htdocs\hofiles\lateinsert.php on line 7
Notice: Undefined index: rentdue in C:\xampp\htdocs\hofiles\lateinsert.php on line 8
Notice: Undefined index: prevbal in C:\xampp\htdocs\hofiles\lateinsert.php on line 9
Notice: Undefined index: hudpay in C:\xampp\htdocs\hofiles\lateinsert.php on line 10
Notice: Undefined index: tentpay in C:\xampp\htdocs\hofiles\lateinsert.php on line 11
Notice: Undefined index: datepaid in C:\xampp\htdocs\hofiles\lateinsert.php on line 12
Notice: Undefined index: comments in C:\xampp\htdocs\hofiles\lateinsert.php on line 13
Notice: Undefined index: paidsum in C:\xampp\htdocs\hofiles\lateinsert.php on line 14
Notice: Undefined index: name in C:\xampp\htdocs\hofiles\lateinsert.php on line 15
Notice: Undefined index: apt in C:\xampp\htdocs\hofiles\lateinsert.php on line 15
Notice: Undefined index: amtpaid in C:\xampp\htdocs\hofiles\lateinsert.php on line 16
Notice: Undefined index: rentdue in C:\xampp\htdocs\hofiles\lateinsert.php on line 17
Notice: Undefined index: prevbal in C:\xampp\htdocs\hofiles\lateinsert.php on line 18
Notice: Undefined index: hudpay in C:\xampp\htdocs\hofiles\lateinsert.php on line 19
Notice: Undefined index: tentpay in C:\xampp\htdocs\hofiles\lateinsert.php on line 20
Notice: Undefined index: datepaid in C:\xampp\htdocs\hofiles\lateinsert.php on line 21
Notice: Undefined index: comments in C:\xampp\htdocs\hofiles\lateinsert.php on line 22
Notice: Undefined index: paidsum in C:\xampp\htdocs\hofiles\lateinsert.php on line 23
data inserted
query: INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal,hudpay,tentpay,datepaid,comments,paidsum) VALUES ('', '', '', '', '', '', '', '', '', '')


#6
ckdoublenecks

ckdoublenecks

    Newbie

  • Members
  • Pip
  • 5 posts
it works now with the below code but with the below message every time:

<?php

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(); 


?>  

Quote

Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\hofiles\lateinsert.php on line 4
data inserted


#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
you can't run the query before you define it???
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You should as well not make the parser guess constants:
mysql_connect(localhost,root,""); should be mysql_connect("localhost", "root", "");
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
As Alexander pointed out, your first problem is in the mysql_connect AND the mysql_select_db statement. The second problem is CLEARLY stated in your logs.

Quote

Notice: Undefined index: rentdue in C:\xampp\htdocs\hofiles\lateinsert.php on line 17

UNDEFINED means it's not there. As Blaine stated, you can do print_r($_POST) to see all of $_POST. Most likely you have a problem further up the line and $_POST is simply not being passed as you think it is. Backtrack by hand through your code to find where it's coming from and make sure things are happening as they are supposed to.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users