Jump to content

Crud Basic Script Help.......


googlit

Recommended Posts

Hi all, im wanting to create a basic update script which pulls data from two tables and then puts data to two tables.

 

i am currently getting :

 

Notice: Undefined index: product_id in edit.php on line 26

SQL: select * from jos_vm_product, jos_vm_product_price WHERE jos_vm_product.product_id AND jos_vm_product_price.product_id= >> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

this is the code i have:

 

my update script which displays the records in the table with a link to the edit form

 

<?php
//include database configuration
include 'config_open_db.php';
//selecting records
$sql="select
jos_vm_product.product_id,
jos_vm_product.product_name,
jos_vm_product.product_s_desc,
jos_vm_product_price.product_id,
jos_vm_product_price.product_price
from
jos_vm_product, jos_vm_product_price";
//query the database
$rs=mysql_query($sql) or die($sql.">>".mysql_error());
//count how many records found
$num=mysql_num_rows($rs);
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";


 echo "<tr>";
	 echo "<th>Product name</th>";
	 echo "<th>Product Tag Line</th>";
	 echo "<th>Product Price</th>";
	 echo "<th>Action</th>";
 echo "</tr>";

 //retrieve ocontents
 while($row=mysql_fetch_array($rs)){

	 extract($row);


	 echo "<tr>";
		 echo "<td>{$product_name}</td>";
		 echo "<td>{$product_s_desc}</td>";
		 echo "<td>{$product_price}</td>";

		 echo "<td>";
			 echo "<a href='edit.php?id={$product_id}'>Edit</a>";
		 echo "</td>";
	 echo "</tr>";
 }
echo "</table>";

}else{ //if no records
echo "No records found.";
}
?>

 

and my edit form (edit.php)

 

<?php
include 'config_open_db.php';

//check if an action was set
isset($_POST['action']) ? $action=$_POST['action'] : $action="";

if($action=="edit"){
 //update the record
 $sql="update jos_vm_product, jos_vm_product_price
		 set
			 product_name='{$_POST['product_name']}',
			 product_s_desc='{$_POST['product_s_desc']}',
			 product_price='{$_POST['product_price']}',
 product_weight={$_POST['product_weight']}'

		 where
			 product_id={$_POST['id']}";
 if(mysql_query($sql)){

	 echo "<div>Record was edited.</div>";
 }else{
	 die("SQL: ".$sql." >> ".mysql_error());
 }
}

$id=$_REQUEST['product_id']; //the user id


$sql="select * from jos_vm_product, jos_vm_product_price WHERE jos_vm_product.product_id AND jos_vm_product_price.product_id={$id}";
$rs=mysql_query($sql) or die("SQL: ".$sql." >> ".mysql_error());
$num=mysql_num_rows($rs);


if($num>0){
 $row=mysql_fetch_assoc($rs);
 extract($row);
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
<table>
 <tr>
	 <td>Product Name</td>
	 <td><input type='text' name='product_name' value='<?php echo $product_name; ?>' /></td>
 </tr>
 <tr>
	 <td>Product Tag Line</td>
	 <td><input type='text' name='product_s_desc' value='<?php echo $product_s_desc; ?>' /></td>
 </tr>
 <tr>
	 <td>Product Price</td>
	 <td><input type='text' name='product_price' value='<?php echo $product_price; ?>' /></td>
 </tr>
 <tr>
	 <td>Product_weight</td>
	 <td><input type='text' name='product_weight' value='<?php echo $product_weight; ?>' /></td>
 <tr>
	 <td></td>
	 <td>
		 <!-- so that we could identify what record is to be updated -->
		 <input type='hidden' name='id' value='<?php echo $id ?>' />

		 <!-- we will set the action to edit -->
		 <input type='hidden' name='action' value='edit' />
		 <input type='submit' value='Edit' />
	 </td>
 </tr>
</table>
</form>
<?php
}else{
 echo "<div>User with this id is not found.</div>";
}
echo "<a href='update.php'>Back To List</a>";
?>

 

i also need to limit the results to 30 and add pages for next 30 results etc but am unsure how....

 

any help will be greatly appreciated..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.