wright67uk Posted March 15, 2013 Share Posted March 15, 2013 <?php include 'connect.php'; $date = date('Y-m-d H:i:s'); $username = "Ed"; $horse = isset($_GET['horse'])&& $_GET['horse']!=''?$_GET['horse']:''; echo $horse; // echos fine $query = "SELECT horse_name, username FROM notes where horse_name = '$horse'"; $result = $mysqli->query($query); //if table is empty then insert should happen below if(!$result){ $query = "INSERT INTO notes SET horse_name='$horse', username='$username', publish_date='$date'"; $results = $mysqli->query($query); } else{ echo 'horse is exists'; } $result->free(); $mysqli->close(); ?> Hello, i'm trying to add some details to a database if no records exist for $horse. At the moment I have an empty table, and nothing is being inserted when I test the code. $horse echo's out fine and all I get is this followed by 'horse exists'. What am I doing wrong here. (I'm new to mysqli so I maybe goig a bit wrong here) I have tested my connection. Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/ Share on other sites More sharing options...
denno020 Posted March 15, 2013 Share Posted March 15, 2013 Maybe you could count the results that are returned instead of just checking if $result exists.. I expect that even if the query 'fails', something is returned by the mysqli object, which is assigned to $result. The old way used to be mysql_num_rows(), but I'm not sure what the new way is with mysqli as I haven't looked into it too much. But that's what I suggest using in the if statement Hope that helps Denno Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418784 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 Yes, denno020 is right and use mysql_num_rows() after the first $result. My question is: What is common between horse and date values? Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418787 Share on other sites More sharing options...
wright67uk Posted March 15, 2013 Author Share Posted March 15, 2013 I'm recording the date a horse was added to the database. Later on I will take notes about each horse from visitors. The date will show how relevant the note will be Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418789 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 I'm recording the date a horse was added to the database. Later on I will take notes about each horse from visitors. The date will show how relevant the note will be Well, but the horse is a beautiful animal, that's why I get confused. You maybe meant hourse? Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418791 Share on other sites More sharing options...
denno020 Posted March 15, 2013 Share Posted March 15, 2013 I think this has something to do with horse races, keeping track of the horses... And what is a hourse? :/ Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418792 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 And what is a hourse? Multiple hours Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418793 Share on other sites More sharing options...
wright67uk Posted March 15, 2013 Author Share Posted March 15, 2013 Lol I did mean horse yes... According to the urban dictionary a hourse is a cop that takes both the side of the criminal and the cops with a price. Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418795 Share on other sites More sharing options...
jcbones Posted March 15, 2013 Share Posted March 15, 2013 You should look into ON DUPLICATE UPDATE. Making sure that you have a primary, or unique key. Quote Link to comment https://forums.phpfreaks.com/topic/275691-mysqli-if-it-doesnt-exist-then-add-it/#findComment-1418840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.