Jump to content

deleting from mysql if statment not working


hyster

Recommended Posts

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.";
}
?>

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.