Jump to content

MySQL in PHP - No syntax errors, but not working :-(


eam768

Recommended Posts

The following code is connecting.. and just the sql part works until i add the variables and radio button input parts... please help!

 

 

<?php
$link = mysql_connect("localhost", "emorette11", "mypassword", "mydatabase")
or die("Could not connect: " . mysql_error($link));
print ("Connected successfully");
mysql_select_db("mydatabase");

echo "<br />";
$color = $_POST["color"];
$material = $_POST["material"];
$gender = $_POST["gender"];
$size = $_POST["size"];
$price = $_POST["price"];
$id = $_POST["id"];

  if (isset($_POST["submit"]))
   {
   if (isset($_POST["record"]))
     {

  $radio = $_POST["record"];

if ($radio=="add")
{
$sql="INSERT INTO EyeGlasses (Color, Material, Gender, Size, Price, ID) VALUES ('$color','$material','$gender','$size','$price','$id')";
 
$result= mysql_query($sql,$link) or die(mysql_error());
$showresult = mysql_query("SELECT * from EyeGlasses")
    or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($showresult))
   {
    echo ("<br> Color = ". $row["Color"] . "<br> Material =  " . $row["Material"] . "<br>");
    echo("Gender = " . $row["Gender"] . "<br> Size = " . $row["Size"] . "<br>");
    echo("Price = " . $row["Price"] . "<br> ID = " . $row["ID"] . "<br>");
   }
}
else if ($radio=="update")
{
$sql="UPDATE EyeGlasses SET Color='$color',Material='$material', Gender='$gender', Size='$size', Price='$price', ID='$id' WHERE ID='$id'";
$result= mysql_query($sql,$link) or die(mysql_error());
$showresult = mysql_query("SELECT * from EyeGlasses")
    or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($showresult))
   {
    echo ("<br> Color = ". $row["Color"] . "<br> Material =  " . $row["Material"] . "<br>");
    echo("Gender = " . $row["Gender"] . "<br> Size = " . $row["Size"] . "<br>");
    echo("Price = " . $row["Price"] . "<br> ID = " . $row["ID"] . "<br>");
   }
}
else
{
$sql="DELETE FROM EyeGlasses WHERE ID='$id'";
$result= mysql_query($sql,$link) or die(mysql_error());
$showresult = mysql_query("SELECT * from EyeGlasses")
    or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($showresult))
   {
    echo ("<br> Color = ". $row["Color"] . "<br> Material =  " . $row["Material"] . "<br>");
    echo("Gender = " . $row["Gender"] . "<br> Size = " . $row["Size"] . "<br>");
    echo("Price = " . $row["Price"] . "<br> ID = " . $row["ID"] . "<br>");
   }
}}}
 ?>

 

Link to comment
Share on other sites

well for one you are missing a semi colon in your coding. :tease-01:

 

Tidied Code:

 <?php
$link = mysql_connect("localhost", "emorette11", "mypassword", "mydatabase") or die("Could not connect: " . mysql_error($link));
print("Connected successfully");
mysql_select_db("mydatabase");
echo "<br />";
$color    = $_POST["color"];
$material = $_POST["material"];
$gender   = $_POST["gender"];
$size     = $_POST["size"];
$price    = $_POST["price"];
$id       = $_POST["id"];
if (isset($_POST["submit"]))
  {
    if (isset($_POST["record"]))
      {
        $radio = $_POST["record"];
        if ($radio == "add")
          {
            $sql = "INSERT INTO EyeGlasses (Color, Material, Gender, Size, Price, ID) VALUES ('$color','$material','$gender','$size','$price','$id')";
            $result = mysql_query($sql, $link) or die(mysql_error());
            $showresult = mysql_query("SELECT * from EyeGlasses") or die("Invalid query: " . mysql_error());
            while ($row = mysql_fetch_array($showresult))
              {
                echo ("<br /> Color = " . $row["Color"] . "<br /> Material =  " . $row["Material"] . "<br />");
                echo ("Gender = " . $row["Gender"] . "<br /> Size = " . $row["Size"] . "<br />");
                echo ("Price = " . $row["Price"] . "<br /> ID = " . $row["ID"] . "<br />");
              }
          }
        else if ($radio == "update")
          {
            $sql = "UPDATE EyeGlasses SET Color='$color',Material='$material', Gender='$gender', Size='$size', Price='$price', ID='$id' WHERE ID='$id'";
            $result = mysql_query($sql, $link) or die(mysql_error());
            $showresult = mysql_query("SELECT * from EyeGlasses") or die("Invalid query: " . mysql_error());
            while ($row = mysql_fetch_array($showresult))
              {
                echo ("<br /> Color = " . $row["Color"] . "<br /> Material =  " . $row["Material"] . "<br />");
                echo ("Gender = " . $row["Gender"] . "<br /> Size = " . $row["Size"] . "<br />");
                echo ("Price = " . $row["Price"] . "<br /> ID = " . $row["ID"] . "<br />");
              }
          }
        else
          {
            $sql = "DELETE FROM EyeGlasses WHERE ID='$id'";
            $result = mysql_query($sql, $link) or die(mysql_error());
            $showresult = mysql_query("SELECT * from EyeGlasses") or die("Invalid query: " . mysql_error());
            while ($row = mysql_fetch_array($showresult))
              {
                echo ("<br /> Color = " . $row["Color"] . "<br /> Material =  " . $row["Material"] . "<br />");
                echo ("Gender = " . $row["Gender"] . "<br /> Size = " . $row["Size"] . "<br />");
                echo ("Price = " . $row["Price"] . "<br /> ID = " . $row["ID"] . "<br />");
              }
          }
      }
  }
?> 
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.