By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

Adding data to a mysql database works one time only

P: 3
Hi I am busy now for many hours without luck

I have a database and when I use PHP to add date to it, it works only one time
when i want to add the next data, It doesn't work. but i don't get any errors???

see my code:
[PHP]
$query = "INSERT INTO products VALUES ('$typedata','$pro_code_ourdata','$pronum','$prona me','$prodetails','$facnum','$picturedata','$price ','$probox','$proquantity')";
mysql_query($query);
[/PHP]

What is wrong here or what is missing?
Please help

You can check it at
www.bs-exportchina.com/products


Here is the full code:
Maybe some code is a bit unusual but that is because I'm new with PHP
[PHP]<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?
$pictureloadok = 0;
$type = $_REQUEST['type'];
$pronum = $_REQUEST['pronum'];
$proname = $_REQUEST['proname'];
$prodetails = $_REQUEST['prodetails'];
$probox = $_REQUEST['probox'];
$proquantity = $_REQUEST['proquantity'];
$facnum = $_REQUEST['facnum'];
$price = $_REQUEST['price'];
If (!empty($type)) {
$typeok = 1;
}else{
$typeok = 0;
}
If (!empty($pronum)) {
$pronumok = 1;
}else{
$pronumok = 0;
}
If (!empty($proname)) {
$pronameok = 1;
}else{
$pronameok = 0;
}
If (!empty($prodetails)) {
$prodetailsok = 1;
}else{
$prodetailsok = 0;
}
If (!empty($probox)) {
$proboxok = 1;
}else{
$proboxok = 0;
}
If (!empty($proquantity)) {
$proquantityok = 1;
}else{
$proquantityok = 0;
}
If (!empty($facnum)) {
$facnumok = 1;
}else{
$facnumok = 0;
}
If (!empty($price)) {
$priceok = 1;
}else{
$priceok = 0;
}
if ($typeok = 1) {
if ($pronumok = 1) {
if ($pronameok = 1) {
if ($prodetailsok = 1) {
if ($proboxok = 1) {
if ($proquantityok = 1) {
if ($facnumok = 1) {
if ($priceok = 1) {
If (!empty($_FILES['uploaded']['name'])){
$target = "databasepictures/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
$pictureloadok = 1;
$picturedata = "/pictures/databasepictures/".$_FILES['uploadedfile']['name'];
}else{
$pictureloadok = 0;
}
}else{
$pictureloadok = 2;
}
}
}
}
}
}
}
}
}
if ($pictureloadok == 1) {
$user="*******";
$password="*******";
$database="********";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `products` ";
mysql_query($query);
$result=mysql_query($query);
$num=mysql_numrows($result) ;
$num--;
$pro_code_ourdata=mysql_result($result,$num,"pro_c ode_our");
$pro_code_ourdata++;
mysql_close();

mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM pro_index ";
mysql_query($query);
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num){
$typedataa=mysql_result($result,$i,"pro_type_code" );
$typedatab=mysql_result($result,$i,"pro_type");
if ($typedatab == $type) {
$typedata = $typedataa;
}
$i++;
}

$picturedata = "products/".$target;
$query = "INSERT INTO products VALUES ('$typedata','$pro_code_ourdata','$pronum','$prona me','$prodetails','$facnum','$picturedata','$price ','$probox','$proquantity')";
mysql_query($query);
mysql_close();
Print $typedata;
print " ";
print $pro_code_ourdata;
print " ";
print $pronum;
print " ";
print $proname;
print " ";
print $prodetails;
print " ";
print $facnum;
print " ";
print "products/".$target;
print " ";
print $price;
print " ";
print $probox;
print " ";
print $proquantity;
print " ";
print "are added to the database";
}

$user="******";
$password="******";
$database="******";
$pro_type_screen = isset($_REQUEST['pro_type_screen']) ? trim($_REQUEST['pro_type_screen']) : "";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `products` ";
mysql_query($query);
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
<table width="*" border="1" cellspacing="0" cellpadding="0">
<?

$i=0;
while ($i < $num) {
$pro_type=mysql_result($result,$i,"pro_type");
$pro_code_our=mysql_result($result,$i,"pro_code_ou r");
$pro_code_fac=mysql_result($result,$i,"pro_code_fa c");
$pro_name=mysql_result($result,$i,"pro_name");
$pro_details=mysql_result($result,$i,"pro_details" );
$pro_fac=mysql_result($result,$i,"pro_fac");
$pro_pic=mysql_result($result,$i,"pro_pic");
$pro_price=mysql_result($result,$i,"pro_price");
$pro_boxsize=mysql_result($result,$i,"pro_boxsize" );
$pro_inbox=mysql_result($result,$i,"pro_inbox");
?>
<tr>
<?
echo"<td width='20' valign='top'>$pro_type</td>";
echo"<td width='75' valign='top'>$pro_code_our</td>";
echo"<td width='75' valign='top'>$pro_code_fac</td>";
echo"<td width='100' valign='top'>$pro_name</td>";
echo"<td width='400' valign='top'>$pro_details</td>";
echo"<td width='20' valign='top'>$pro_fac</td>";
echo"<td width='400' valign='top'>$pro_pic</td>";
echo"<td width='150' valign='top'>$pro_price</td>";
echo"<td width='100' valign='top'>$pro_boxsize</td>";
echo"<td width='50' valign='top'>$pro_inbox</td>";
?>
</tr>
<?
$i++;
}
?>
</table>
<?
mysql_close();
?>
<form name="form1" ENCTYPE="multipart/form-data" method="post" action="index.php">
<?
$user="******";
$password="******";
$database="******";
$pro_id=$_POST['pro_id'];
$pro_type=$_POST['pro_type'];
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM pro_index";
mysql_query($query);
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
?>
Product type
<select name="type" size="1">
<?
while ($i < $num) {
$pro_id=mysql_result($result,$i,"pro_type_code");
$pro_type=mysql_result($result,$i,"Pro_type");
echo "<option>$pro_type</option>";
$i++;
}
mysql_close();
?>
</select>
<?
$pro_code_our++;
Echo "<br>Product number = BS$pro_code_our </p>";
?>
<br>
Product number from Factory
<input type="text" name="pronum">
<br>
Product name
<input type="text" name="proname">
<br>
Product details
<input type="text" name="prodetails" size="50">
<br>
Box size
<input type="text" name="probox">
<br>
Quantity
<input type="text" name="proquantity">
<br>
Picture
<INPUT NAME="uploaded" TYPE="File">

<br>
Factory number
<input type="text" name="facnum">
<br>
Buy Price
<input type="text" name="price">
<br>
<input type="submit" name="Submit" value="Add record">
<br>
</form>
<p>&nbsp;</p>
</body>
</html>
[/PHP]
Mar 25 '07 #1
Share this Question
Share on Google+
2 Replies


ronverdonk
Expert 2.5K+
P: 4,258
What do you mean by
I have a database and when I use PHP to add date to it, it works only one time when i want to add the next data, It doesn't work. but i don't get any errors???
1. How do you know it does not work? How did you verify that?

2. You don't get any errors because you do not trap the error message. Change your statement to
Expand|Select|Wrap|Line Numbers
  1. $r=mysql_query($query)
  2.    or die ("INSERT error: ".mysql_error());
and see what error message is returned.

Ronald :cool:
Mar 25 '07 #2

P: 3
Greet, I didn't know that.

I got the massage INSERT error: Duplicate entry '1003' for key 1

I have 2 rows added with Myphpadmin.
the 3rd Row was added with my script. but wil add the row at the begining and not at the end. my script will check the last row not the first.

Thanks for the advise
Mar 26 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.