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
https://forums.phpfreaks.com/topic/116709-solved-small-phpsql-problem/
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.

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

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"] . "')");
}

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

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.