hi,
I am trying to save some textbox array into database , i have write some code but it giving error, i have searched but couldn’t find the reason of error.
Can you help me out in this.
<?php
require_once("config.php");
if(isset($_POST['save'])){
//$slno=$_POST['slno'];
//echo "hello"." ".$slno;
$customer_id="1";
$customer_name="Mr.Rajesh";
$customer_user_name="rkr";
$date=date('d-m-Y');
$time=date('h:m:i:A');
$post_count = count($_POST['slno']);
$sl_no = array();
$album_name = array();
$paper_used = array();
$finishing = array();
$price = array();
$vat = array();
$total_amt = array();
$sl_no = $_POST['slno'];
$album_name = $_POST['album_name'];
$paper_used = $_POST['paper_used'];
$finishing = $_POST['finishing'];
$price = $_POST['price'];
$vat = $_POST['vat'];
$total_amt = $_POST['total_amt'];
for ($i = 0; $i <= $post_count; $i++)
{
$sql[] = "INSERT INTO customer_album_price(customer_id, customer_name, customer_user_name, sl_no, album_name, paper, finish, price, vat, total_amt, date, time) VALUES ($customer_id','$customer_name','$customer_user_name','".$sl_no[$i]."','".$album_name[$i]."','".$paper_used[$i]."','".$finishing[$i]."','".$price[$i]."','".$vat[$i]."','".$total_amt[$i]."','$date','$time')";
}
foreach ($sql as $query)
{
mysqli_query($con, $query);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body >
<form id="registration" name="registration" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<?php
$query_for_result=mysqli_query($con,"SELECT * FROM album ORDER BY id ASC");
$num=mysqli_num_rows($query_for_result);
mysqli_close($con);
?>
<table >
<tr>
<td>Sl.No..</td>
<td >Item</td>
<td >Paper</td>
<td >Finish</td>
<td >Price</td>
<td >Vat</td>
<td >Total Amount</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_assoc($query_for_result))
{
$f1=$row['id'];
$f2=$row['album_name'];
$f3=$row['paper_used'];
$f4=$row['finishing'];
$f5=$row['price'];
$f6=$row['vat'];
$f7=$row['total_amt'];
$id=$i+1;
//echo $id;
?>
<tr>
<td><input id="slno" name="slno[]" value="<?php echo $f1; ?>" type="text" style="width:20px;"/></td>
<td><input id="album_name" name="album_name[]" value="<?php echo $f2; ?>" type="text" style="width:150px;"/></td>
<td><input id="paper_used" name="paper_used[]" type="text" value="<?php echo $f3; ?>" style="width:85px;"/></td>
<td><input id="finishing" name="finishing[]" value="<?php echo $f4; ?>" type="text" style="width:90px;"/></td>
<td><input id="price" name="price[]" value="<?php echo $f5; ?>" type="text" style="width:25px;"/></td>
<td><input id="vat" name="vat[]" type="text" value="<?php echo $f6; ?>" style="width:35px;"/></td>
<td><input id="total_amt" name="total_amt[]" value="<?php echo $f7; ?>" type="text" style="width:50px;"/></td>
</tr>
<?php
$i++;
}
?>
<tr>
<td colspan="7" ><input name="save" value="Save" id="save" type="submit" style="width:83px;" /></td>
</tr>
</table>
</form>
</body>
</html>
Thanks
Try these debugging tips:
<php
if(true)
{
// display $_POST variables
echo '<pre>';
print_r($_POST);
echo '</pre>';
// ensure EVERY $_POST variables has a value
$album_name = isset($_POST['album_name']) ? $_POST['album_name'] : 'NOT SET';
$paper_used = isset($_POST['paper_used']) ? $_POST['paper_used'] : 'NOT SET';
$total_amt = isset($_POST['total_amt']) ? $_POST['total_amt'] : 'NOT SET';
// continue for all other variables
}
Hi John_Betong,
Thanks for the reply.
the values for
[25] [26] [27] [28] (in array of 0 to 28) every fields except slno will be blank by default. so will it cause the problem? rest in all 0 to 24 values are properly coming.
Thanks
Amend these lines so you can see what $_POST variables are set and which ones are missing:
if( isset($_POST) )
{
// display $_POST variables
echo '<pre>';
print_r($_POST);
echo '</pre>';
die;
}
// ALSO
echo '<pre>';
print_r($sql);
echo '</pre>';
hi John_Betong ,
As i mention earlier also, i know which one will be blank (in last 4 rows these album_name, paper, finish, price, vat, total_amt fields will be blank).
But i want to know how to remove this error.
thanks
Try print_r($sql[$i]); and see which $i is giving problems. Once you know use an if() statement to bypass the faulty values.
Hi John_Betong,
as i use print_r($sql[$i]); instead of print_r($sql); it giving the error for (print_r($sql[$i]);).
But i change the loop
from
for ($i = 0; $i <= $post_count; $i++)
to
for ($i = 0; $i < $post_count; $i++)
No error coming but when i check to database , i can’t see any values stored there.
Thanks
Inside the $sql loop echo the $query and also check the return value of mysql_query()
// from the php manual online
$result[COLOR=#0000BB][FONT=Source Code Pro] [/FONT][/COLOR][COLOR=#007700][FONT=Source Code Pro]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Source Code Pro]mysql_query[/FONT][/COLOR][COLOR=#007700][FONT=Source Code Pro]([/FONT][/COLOR][COLOR=#DD0000][FONT=Source Code Pro]'SELECT * WHERE 1=1'[/FONT][/COLOR][COLOR=#007700][FONT=Source Code Pro]);
if (![/FONT][/COLOR][COLOR=#0000BB][FONT=Source Code Pro]$result[/FONT][/COLOR][COLOR=#007700][FONT=Source Code Pro]) {
die([/FONT][/COLOR][COLOR=#DD0000][FONT=Source Code Pro]'Invalid query: ' [/FONT][/COLOR][COLOR=#007700][FONT=Source Code Pro]. [/FONT][/COLOR][COLOR=#0000BB][FONT=Source Code Pro]mysql_error[/FONT][/COLOR][COLOR=#007700][FONT=Source Code Pro]());
}
[/FONT][/COLOR]
Hi,
I used the below code
for ($i = 0; $i < $post_count; $i++)
{
$sql[] = "INSERT INTO pro_customer_album_price(customer_id, customer_name, customer_user_name, sl_no, album_name, paper, finish, price, vat, total_amt, date, time) VALUES ($customer_id','$customer_name','$customer_user_name','".$sl_no[$i]."','".$album_name[$i]."','".$paper_used[$i]."','".$finishing[$i]."','".$price[$i]."','".$vat[$i]."','".$total_amt[$i]."','$date','$time')";
}
foreach ($sql as $query)
{
$data = mysqli_query($con, $query);
if(!$data)
{
echo "Error".mysqli_connect_error();// this one coming by default.
}
else
{
echo "Inserted!";
}
}
but it shows error on page load (29 times) instead the error should show in $_post[‘save’] event.
Thanks
hi,
Here is some correction, the error code coming after $_post[‘save’]
Thanks
Is any data being saved?
What is the error?
Have you Googled for a solution?
Hi,
Actually this part of code is coming.
if(!$data)
{
echo "Error".mysqli_connect_error();// this one coming by default.
}
no data being saved in database.
i am not able to find why the error coming, when i will know about the error then i can only search for that.
What version of MySQL?
Both date and time are reserved words https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
so if you have < ver. 5.5 the queries will fail if those field names are unquoted.