misdigest
misdigest

Reputation: 37

If text box is empty choose option and insert to table

I am working on a recipe website one of the fields on the recipe upload page gives the user the option to either upload a value via a dropdown menu or add a new one via a text box

I have made it work so when the dropdown has no value it will choose the text however this does not work the other way round I'm getting this error:

Insert failed: 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 '30'>English

Here is the insert code:

if($cuisine==""){
    $query="INSERT INTO`recipename`(cuisine_type)VALUES('$cuisinetype')";
    mysqli_select_db($db_server, $db_database);
    mysqli_query($db_server, $query) or die("Insert failed: " . mysqli_error($db_server)) ;
}
//check whether the cuisine type exists//
if ($cuisinetype !='empty'){
    $query="SELECT cuisine_type FROM `Nation` WHERE cuisine_type='$cuisine'";
    $result=mysqli_query($db_server, $query) ;
    if ($row = mysqli_fetch_array($result)){
        $message = "Sorry we already have that one!";
}else{
    $query = "INSERT INTO`Nation`(cuisine_type)VALUES('$cuisine')";
    mysqli_select_db($db_server, $db_database);
    mysqli_query($db_server, $query) or die("Insert failed: " . mysqli_error($db_server)) ;
    }
}

Upvotes: 0

Views: 459

Answers (1)

Denys Vitali
Denys Vitali

Reputation: 530

$query = "INSERT INTO `Nation` (cuisine_type) VALUES ('$cuisine')";

Add spaces!

Anyway, please, take a look at this to prevent SQL Injections, just in case you've missed it.

Upvotes: 1

Related Questions