Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Can't Submit Data To Database

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Can't Submit Data To Database

Postby digip » Mon Oct 08, 2012 9:36 pm

Hi Absolute Newb here,

I am having trouble submitting my data to mysql database.
"Error inserting data" - comes from the code written in below.

Can anyone help? Thank you!


<html>
<body>
<h2>Enter Company Invoice</h2>
<STYLE>
.error {padding: 10px; color: #cc0000; font-weight: bold;}
</STYLE>

<?
if ($_POST) {
$msg = "";

foreach($_POST as $k => $v) {
$v = trim($v) ;
$k = $v ;

if ($v=="") {
$msg = "Please fill in both fields Bec!";
}
}

if ($msg=="") {
$insert = "INSERT INTO digitalp_receivables
(company, invoice_number, amount, due_date)
VALUES ('$co', '$inv', '$amt', '$dd')";

$link =
mysqli_connect('localhost','digitalp_rec','6Xl0T@ZB,{~f','digitalp_receivables');


if (!mysqli_query($link, $insert)) {
$msg = "Error inserting data";
} Else {
$msg = "Record successfully added";

$co = $inv = $amt = $dd = "";
}
mysqli_close($link);
}

echo "<div class=\"error\">$msg</div>";

} else {
$co = $inv = $amt = $dd = "";
}
?>

<FORM METHOD="post" ACTION="<? echo
$_SERVER['PHP_SELF']?>">
<TABLE BORDER="1" CELLPADDING="5">
<TR>
<TH>Company</TH>
<TH>Invoice</TH>
<TH>Amount</TH>
<TH>Due</TH>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="$co" VALUE="<?
echo $co ?>" /></TD>
<TD><INPUT TYPE= "text" NAME="$inv" VALUE="<?
echo $inv ?>" /></TD>
<TD><INPUT TYPE= "text" NAME="$amt" VALUE="<?
echo $amt ?>" /></TD>
<TD><INPUT TYPE= "text" NAME="$dd" VALUE="<?
echo $dd ?>" /></TD>
</TR>
</TABLE>
<BR/>
<INPUT TYPE= "SUBMIT" VALUE= "Add Invoice"/>
<INPUT TYPE= "RESET" VALUE="Cancel"/>
</FORM>



</body>
digip
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Oct 08, 2012 9:29 pm

Re: Can't Submit Data To Database

Postby simplypixie » Mon Oct 08, 2012 10:41 pm

Two things are fundamentally wrong with your code:

1. You are overwriting your $k variable with your $v value in the foreach loop

2. You are not assigning any values to the variables in your update query - VALUES ('$co', '$inv', '$amt', '$dd')";

So I would change your foreach to just check for errors:

Code: Select all
foreach($_POST as $k => $v) {
$v = trim($v) ;

if ($v=="") {
$msg = "Please fill in both fields Bec!";
}
}


Then just use your $_POST variables in your query and add in a query check:
Code: Select all
$insert = "INSERT INTO digitalp_receivables
(company, invoice_number, amount, due_date)
VALUES ('".$_POST['co']."','".$_POST['inv'].",'".$_POST['amt']."','".$_POST['dd']."') or die(mysql_error())";
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Can't Submit Data To Database

Postby digip » Mon Oct 08, 2012 11:59 pm

Wow... that's very cool, such a quick response.
Thanks for your help, I'll see how I go.
digip
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Oct 08, 2012 9:29 pm

Re: Can't Submit Data To Database

Postby digip » Tue Oct 09, 2012 12:33 am

Hi ... I made the changes you suggested and still came back the same.

I was following a tutorial!
digip
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Oct 08, 2012 9:29 pm

Re: Can't Submit Data To Database

Postby simplypixie » Tue Oct 09, 2012 3:01 am

Have you printed out the posted data? If so is it all correct?

Did you get an error from the query?
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Can't Submit Data To Database

Postby digip » Thu Oct 11, 2012 12:44 am

No, there was no error script - only the "Error inserting data" which is in the code below.

$co = $inv = $amt = $dd = ""; - This is the only bit I have changed ... in the tutorial he had only 2 variables. - $his_example1 = $his_example2 = "";


if (!mysqli_query($link, $insert)) {
$msg = "Error inserting data";
} Else {
$msg = "Record successfully added";

$co = $inv = $amt = $dd = "";
}
mysqli_close($link);
}

Thanks
digip
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Oct 08, 2012 9:29 pm

Re: Can't Submit Data To Database

Postby seandisanti » Thu Oct 11, 2012 7:59 am

you're building your query wrong. '$whatever' doesn't return the value stored in the variable $whatever, it returns the literal string "$whatever". change this line
Code: Select all
$insert = "INSERT INTO digitalp_receivables
(company, invoice_number, amount, due_date)
VALUES ('$co', '$inv', '$amt', '$dd')";


to read

Code: Select all
$insert = "INSERT INTO digitalp_receivables
(company, invoice_number, amount, due_date)
VALUES ('".$co."', '".$inv."','".$amt."', '".$dd."')";

or
Code: Select all
$insert = "INSERT INTO digitalp_receivables
(company, invoice_number, amount, due_date)
VALUES (\'$co\', \'$inv\', \'$amt\', \'$dd\')";
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Alexa [Bot], Bing [Bot], mweingand and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron