Jump to content

[SOLVED] oops just added 30,000 rows with no info :(


bhavin_85

Recommended Posts

hey guys

 

as the title says, i created an insert query and its added over 30,000 enteries into my table  :o

 

The first bit of the insert works ok, its only inserted 1 invoice into the invoice table, but the invoice_items table just insert a shed load of empty rows  ???

<?
$cust_id=$_POST['cust_id'];
$date=$_POST['date'];
$item_item_id=$_POST['item_item_id'];
$description=$_POST['description'];
$weight=$_POST['weight'];
$price=$_POST['price'];
$points=($price*0.1);
include('../../config.php');

$sql="INSERT INTO invoices VALUES (0,'$cust_id','$date')";
$result = mysql_query($sql) or die(mysql_error());
$lastid=mysql_insert_id($dbh);

while ($_POST['item_item_id'] != NULL)
{
$sql1="INSERT INTO invoice_items VALUES (0,'$lastid', '$item_item_id', '$description', '$weight', '$price', '$points')";
$result1 = mysql_query($sql1) or die(mysql_error());
}

?>

 

the data come from a form where the employees enter customer invoices

layout

 

Invoice Details:

Cust_id:

Date:

 

Items:

Item    Desc  Weight  Price

(multiple form rows to insert multiple items) the page as 5 rows, so if they only fill in 2 it should only add 2 rows to the database

 

can any 1 help?  ???

This is an infinite loop:

<?php
while ($_POST['item_item_id'] != NULL)
{
$sql1="INSERT INTO invoice_items VALUES (0,'$lastid', '$item_item_id', '$description', '$weight', '$price', '$points')";
$result1 = mysql_query($sql1) or die(mysql_error());
}
?>

since there is no mechanism to increment "$_POST['item_item_id']". You probably want to do something like this:

<?php
for ($i=0;$i<count($_POST['item_item_id']);$i++) {
      $item_item_id=$_POST['item_item_id'][$i];
      $description= mysql_real_escape_string(stripslashes($_POST['description'][$i]));
      $weight=$_POST['weight'];
      $price=$_POST['price'];
      $points=($price*0.1);
      $sql1="INSERT INTO invoice_items VALUES (0,'$lastid', '$item_item_id', '$description', '$weight', '$price', '$points')";
      $result1 = mysql_query($sql1) or die(mysql_error());
}
?>

 

Ken

 

i jsut realised that because i was using item_item_id as the factor which determines if the field is empty it would cause an infinate loop  ::) the item_item_id is pulled from the database on each new so it will just loop over and over

 

im having some trouble though, the values are not transfering from the form to the sql statement  ??? ive tried to echo the variables but not getting any values at all, ive got my method as post so i dont know what it could be

the form code is kinda long: sorrrry

 

<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}
include('../../config.php');

$sql="SELECT * FROM item";
$query=mysql_query($sql) or die(mysql_error());
?>

<html>
<head>
<title>Administrator - Add Sales Invoice</title>
</head>
<body>
<table width="650" align="center">
<form name="add" id="add" method="post" action="add.php">
<tr>
<td>
<?
include('../menu.php');
?></td></tr>
<tr>
<td align="center"><b>
Add Sales Invoice</b></td>
</tr>
</table>
<table align="center" cols="4" id="add">
<tr>
<td colspan="4"><b>Invoice Details:</b></br></br>
Customer ID: <input name="cust_id" id="cust_id" type="text"></input></br></br>
Date: (YYYY-MM-DD)<input name="date" id="date" type="text"></input></br></br>
</td>
</tr>
</table>
<table align="center" cols="4" id="add">
<tr>
<td colspan="4" align="left"><b>
Items:</b></td></tr>
<tr>
<td colspan="1">Item</td>
<td colspan="1">Description</td>
<td colspan="1">Weight</td>
<td colspan="1">Price</td>
</tr>
<tr>
<td colspan="1">
<select type="text" id="item_item_id" name="item_item_id">
<? while ($row = mysql_fetch_array($query))
{?>
<option value="<? printf($row["item_id"]); ?>"><? printf($row["item_name"]);?></option>
<? } mysql_data_seek($query, 0);?>
</select>
</td>
<td colspan="1">
<input id="description" name="description" type="text"></input></td>
<td colspan="1">
<input name="weight" id="weight" type="text"></input></td>
<td colspan="1">
<input name="price" id="price" type="text"></input></td>
</tr>
<tr>
<td colspan="1">
<select type="text" id="item_item_id" name="item_item_id">
<? while ($row1 = mysql_fetch_array($query))
{?>
<option value="<? printf($row1["item_id"]); ?>"><? printf($row1["item_name"]);?></option>
<? } mysql_data_seek($query, 0);?>
</select>
</td>
<td colspan="1">
<input id="description" name="description" type="text"></input></td>
<td colspan="1">
<input name="weight" id="weight" type="text"></input></td>
<td colspan="1">
<input name="price" id="price" type="text"></input></td>
</tr>

<tr>
<td colspan="1">
<select type="text" id="item_item_id" name="item_item_id">
<? while ($row = mysql_fetch_array($query))
{?>
<option value="<? printf($row["item_id"]); ?>"><? printf($row["item_name"]);?></option>
<? } mysql_data_seek($query, 0); ?>
</select>
</td>
<td colspan="1">
<input id="description" name="description" type="text"></input></td>
<td colspan="1">
<input name="weight" id="weight" type="text"></input></td>
<td colspan="1">
<input name="price" id="price" type="text"></input></td>
</tr>
<tr>
<td colspan="1">
<select type="text" id="item_item_id" name="item_item_id">
<? while ($row = mysql_fetch_array($query))
{?>
<option value="<? printf($row["item_id"]); ?>"><? printf($row["item_name"]);?></option>
<? } mysql_data_seek($query, 0); ?>
</select>
</td>
<td colspan="1">
<input id="description" name="description" type="text"></input></td>
<td colspan="1">
<input name="weight" id="weight" type="text"></input></td>
<td colspan="1">
<input name="price" id="price" type="text"></input></td>
</tr>
<tr>
<td colspan="1">
<select type="text" id="item_item_id" name="item_item_id">
<? while ($row = mysql_fetch_array($query))
{?>
<option value="<? printf($row["item_id"]); ?>"><? printf($row["item_name"]);?></option>
<? } mysql_data_seek($query, 0); ?>
</select>
</td>
<td colspan="1">
<input id="description" name="description" type="text"></input></td>
<td colspan="1">
<input name="weight" id="weight" type="text"></input></td>
<td colspan="1">
<input name="price" id="price" type="text"></input></td>
</tr>
<tr><td>
<input id="submit" type="submit" value="Submit"></input>
</td>
</tr>
</form>
</table>
</body>
</html>

Also,

$sql="INSERT INTO invoices VALUES (0,'$cust_id','$date')";

 

Should be either this, which passes null to the aoto_inc id field

$sql="INSERT INTO invoices VALUES (null, '$cust_id', '$date')";

 

or this, leaving it out altogether

$sql="INSERT INTO invoices (cust_id, date) VALUES ('$cust_id','$date')";

 

Same goes for invoice item insert

 

ill change that in the querry, but ive tried to echo the variables from the form and its not transfering them over  ??? im really confused with this i cant pin point where im going wrong!

 

<?
$cust_id=$_POST['cust_id'];
$date=$_POST['date'];
$item_item_id=$_POST['item_item_id'];
$description=$_POST['description'];
$weight=$_POST['weight'];
$price=$_POST['price'];
$points=($price*0.1);
include('../../config.php');

echo $cust_id;
echo $item_item_id;
echo $description;
//$sql="INSERT INTO invoices VALUES (0,'$cust_id','$date')";
//$result = mysql_query($sql) or die(mysql_error());
//$lastid=mysql_insert_id($dbh);

//while ($_POST['description'] != NULL)
//{
//$sql1="INSERT INTO invoice_items VALUES (0,'$lastid', '$item_item_id', '$description', '$weight', '$price', '$points')";
//$result1 = mysql_query($sql1) or die(mysql_error());
//}
//for ($i=0;$i<count($_POST['description']);$i++) {
//      $item_item_id=$_POST['item_item_id'][$i];
//      $description= mysql_real_escape_string(stripslashes($_POST['description'][$i]));
//      $weight=$_POST['weight'];
//      $price=$_POST['price'];
//      $points=($price*0.1);
//      $sql1="INSERT INTO invoice_items VALUES (0,'$lastid', '$item_item_id', '$description', '$weight', '$price', '$points')";
//      $result1 = mysql_query($sql1) or die(mysql_error());
//}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.