Jump to content

[SOLVED] Small PHP/SQL Problem


grahamb314

Recommended Posts

Hi all,

 

I want to check to see if "Show" (From the previous form) is already in the database I have. If it is, then echo something, if it isnt, then add it

 

 

I think i`m nearly there !

<?php
require_once 'mysql_connect.php';
$result = mysqli_query($mysqli, "SELECT show FROM shows WHERE show= '".$_POST["Show"]."'");

if (mysqli_num_rows($result) == 1) {
echo "Show name already exists";
}
	else {$sql = mysqli_query($mysqli, "INSERT INTO `grahamb_test`.`shows` (`id`, `show`) VALUES (NULL,'".$_POST["Show"]."')");
	}
?>

 

Thanks!

 

Graham

 

Link to comment
Share on other sites

try changing it to

$query = "SELECT * FROM `shows` WHERE `show` = '{$_POST["Show"]}';";
$result = mysqli_query($mysqli, $query) or die("Query:{$query} <br>Error:".mysqli_error($mysqli));

are you sure that there is something in $_POST["Show"] even though that shouldn't matter

 

Scott.

Link to comment
Share on other sites

Thanks again,

 

I put something and show and get

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show FROM shows WHERE show= '123'' at line 1

 

Let me try your suggestion

Link to comment
Share on other sites

i copied your code and added a little output what happens now

require_once 'mysql_connect.php';
$query = "SELECT * FROM `grahamb_test`.`shows` WHERE `show` = '{$_POST["Show"]}';";
$result = mysqli_query($mysqli, $query) or die("Query:{$query} <br>Error:".mysqli_error($mysqli));
echo "rows = ".mysqli_num_rows($result);
if (mysqli_num_rows($result) == 1) {
echo "Show name already exists";
} else {
$sql = mysqli_query($mysqli, "INSERT INTO `grahamb_test`.`shows` (`id`, `show`) VALUES (NULL,'" . $_POST["Show"] . "')");
}

Link to comment
Share on other sites

Interesting!  mysql_connect.php just connects to the database (With user and pass) - It's a seperate php page that I call in all Database related php pages

 

All I can say now is thank's for your help. It's people like you that keep people wanting to learn! - If it wasnt for forums like this, I'd have given up on PHP a while ago!

 

Thanks again!

 

Graham

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.