|
Posted by Maiku Mori on 04/16/07 05:06
On Apr 16, 4:54 am, "shotokan99" <soft_devj...@yahoo.com> wrote:
> hi pipz,
>
> im working on a simple cart, whenever the user click on "add to cart"
> link i store the data to mysql. then i display these records using a
> table and each row i place "remove" link.
>
> my problem is if the user click "remove" how will i tell mysql what
> record to delete?
>
> this is how i display the contents of the basket of the user...
> --------------------------------
> <?php
> $x=0;
> $total=0;
> while($x<$num){
> echo '<tr>';
> $type=mysql_result($result,$x,'citemtype');
> $item=mysql_result($result,$x,'cref');
> $price=mysql_result($result,$x,'cprice');
> $total=$total+$price;
>
> echo '<td width="45" align="center" height="12"><b>';
> echo '<font face="Verdana" size="1">'.$type.'</font></b></td>';
> echo '<td width="131" align="center" height="12"><b>';
> echo '<font face="Verdana" size="1">'.$item.'</font></b></td>';
> echo '<td width="54" align="center" height="12"><b>';
> echo '<font face="Verdana" size="1">'.$price.'</font></b></td>';
> echo '<td width="98" align="center" height="12"><b>';
> echo '<font face="Verdana" size="1"><a href="rem.php" style="text-
> decoration: none">remove</a></font></b></td>';
>
> $x++;}//end while
>
> ?>
> ---------------
>
> this part is where the user would like to remove a certain item:
> echo '<font face="Verdana" size="1"><a href="rem.php" style="text-
> decoration: none">remove</a>
>
> currently the contents of rem.php is these:
> -------
> <html>
> <head>
> </head>
> <body>
> <?php
> session_start();
> $user = $_SESSION['myuser'];
> $conn=mysql_connect(.....) or die('server not found');
> $db='mydb';
> mysql_select_db($db) or die('.: database done exist :.');
> $sel="delete from cart where cuser = '$user'";
> $result=mysql_query($sel) or die('.: can\'t perform the query :.');
> mysql_close();
> ?>
> <script type="text/javascript">
> window.location="basket.php";
> </script>
> </body>
> </html>
>
> since i dont know yet how to remove an item individually..i erase them
> all.
Add ID field to DB table and set it to auto_increment , now each time
you add new record to the DB table, it will have unique id. To delete
just pass ID to the remove script via GET or POST: remove.php?id=1 ,
query should look like this: delete from cart where cuser = '$user'
AND id = '$id'
You may want to look into this: http://www.tizag.com/phpT/postget.php
Don't forget to check all user input before using it with DB table.
~Maiku
Navigation:
[Reply to this message]
|