hyster Posted June 10, 2011 Share Posted June 10, 2011 i created a page that can update, insert and delete records. i combined 3 files i use to run the query's into 1 and separated them by if - elseif's snd used $_get to decide which query to run. the delete code workd fine in its on script but when its combined with the other 2 it throws an error. if i echo the $sql i can put it directly in2 phpmyadmin and the query works fine. im stumped on this 1 portion that fails in the combined script but works fine by itself if ($_GET['query'] =="delete"){ $id=$_GET['id']; // Delete data in mysql from row that has this id $sql1="DELETE FROM site_links WHERE id='$id'"; $result1=mysql_query($sql1); if($result1){ echo "Deleted Successfully"; } else { echo "ERROR. no records where deleted"; echo $result1; } } full script <?php //header( 'refresh: 1; url=search.php' ); include("config.php"); $tbl_name="site_links"; // Table name if ($_GET['query'] =="delete"){ $id=$_GET['id']; // Delete data in mysql from row that has this id $sql1="DELETE FROM site_links WHERE id='$id'"; $result1=mysql_query($sql1); if($result1){ echo "Deleted Successfully"; } else { echo "ERROR. no records where deleted"; echo $result1; } } elseif ($_GET['query'] =="update"){ $id = $_POST['id']; $dept = $_POST['dept']; $menu = $_POST['menu']; $header = $_POST['header']; $link_order = $_POST['link_order']; $use_link = $_POST['use_link']; $link1 = $_POST['link1']; $sub_link2 = $_POST['sub_link2']; $sub_link3 = $_POST['sub_link3']; $who_added= $_POST['who_added']; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // upsub_link2 data in mysql database $sql="UPdate site_links SET dept='$dept', menu='$menu', who_added='$who_added', header='$header', link_order='$link_order', use_link='$use_link', link1='$link1', sub_link2='$sub_link2', sub_link3='$sub_link3', sub_link2='$sub_link2' WHERE id='$id'"; $result=mysql_query($sql); // if successfully upsub_link2d. if($result){ echo "Update Successfully"; } else { echo "ERROR. no records where updated"; } } elseif($_GET['query'] == "insert"){ $dept = $_POST['dept']; $menu = $_POST['menu']; $header = $_POST['header']; $link_order = $_POST['link_order']; $use_link = $_POST['use_link']; $link1 = $_POST['link1']; $sub_link2 = $_POST['sub_link2']; $sub_link3 = $_POST['sub_link3']; $url = $_POST['url']; $who_added = $_POST['who_added']; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // upsub_link1 data in mysql database $sql="INSERT INTO $tbl_name ( dept, menu, header, link_order, use_link, link1, sub_link2, sub_link3, url, who_added) values ('$dept', '$menu', '$header', '$link_order', '$use_link', '$link1', '$sub_link2', '$sub_link3', '$url', '$who_added')"; $result=mysql_query($sql); // if successfully upsub_link1d. if($result){ echo "Update Successful"; } else { echo "ERROR. no records where inserted"; } } else{ echo "error. no query was run."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238962-deleting-from-mysql-if-statment-not-working/ Share on other sites More sharing options...
hyster Posted June 10, 2011 Author Share Posted June 10, 2011 my bad. i had the connection after the delete query Quote Link to comment https://forums.phpfreaks.com/topic/238962-deleting-from-mysql-if-statment-not-working/#findComment-1227876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.