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

 

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

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.