eam768 Posted May 25, 2013 Share Posted May 25, 2013 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 https://forums.phpfreaks.com/topic/278391-mysql-in-php-no-syntax-errors-but-not-working/ Share on other sites More sharing options...
darkfreaks Posted May 26, 2013 Share Posted May 26, 2013 well for one you are missing a semi colon in your coding. 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 https://forums.phpfreaks.com/topic/278391-mysql-in-php-no-syntax-errors-but-not-working/#findComment-1432321 Share on other sites More sharing options...
darkfreaks Posted May 26, 2013 Share Posted May 26, 2013 also you can shorten your code abit using ternary operators instead of abunch of IF's. $submit = isset($_POST['submit']) ? $_POST['submit'] : ''; $radio = isset($_POST['record']) ? $_POST['record'] : ''; Link to comment https://forums.phpfreaks.com/topic/278391-mysql-in-php-no-syntax-errors-but-not-working/#findComment-1432325 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.