PAGO Posted March 12, 2014 Share Posted March 12, 2014 (edited) Hey so I am back with another question: I want to make an INSERT in my DB, but only if 2 entries doesn't already exist. $select1 = mysqli_query($con,"SELECT COUNT(email) FROM kunde WHERE email = '$email'"); $select2 = mysqli_query($con,"SELECT COUNT(adresse) FROM kunde WHERE adresse = '$adresse'"); if ($select1 = 0 && $select2 = 0) { $sqlkunde ="INSERT INTO kunde (kunde_geschlecht, vorname, nachname, email, adresse) VALUES ('$geschlecht', '$vorname', '$nachname', '$email', '$adresse')"; mysqli_query($con,$sqlkunde); } So I let my code count how often my variable already exists in in the table. If the variable ($email, $adresse) doesn't exist, the outcome of my count should be 0 and he executes the INSERT. If not he does nothing. The INSERT works, so the problem has to do with the value which stands (.. or doesn't stand) behind my variables $select1/2. Thanks in advance, PAGO Edited March 12, 2014 by PAGO Quote Link to comment https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/ Share on other sites More sharing options...
Solution Ch0cu3r Posted March 12, 2014 Solution Share Posted March 12, 2014 (edited) mysqli_query only returns a result set, you need to use a mysqli_fetch_* function to retrieve data from that result set, eg $result1 = mysqli_query($con,"SELECT COUNT(email) FROM kunde WHERE email = '$email'"); $select1 = mysqli_fetch_row($result1); $result2 = mysqli_query($con,"SELECT COUNT(adresse) FROM kunde WHERE adresse = '$adresse'"); $select2 = mysqli_fetch_row($result2); if($select1[0] != 0 && $select2[0] != 0) { // do insert } Alternatively you could make the email and adresse fields unique keys, then use a an INSERT IGNORE query instead Edited March 12, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/#findComment-1472349 Share on other sites More sharing options...
PAGO Posted March 12, 2014 Author Share Posted March 12, 2014 Yes i thought something like that. It still doesn't work. Why are you writing "$select1[0]", what does the 0 stand for? I always thought, that i have to entere the column here. Quote Link to comment https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/#findComment-1472362 Share on other sites More sharing options...
PAGO Posted March 12, 2014 Author Share Posted March 12, 2014 (edited) I don't know why, but suddently it works. I forgot [] always means an array . I changed the = to != and placed the INSERT in the ELSE section. Before I had $select1[0] = 0 and had the INSERT in the TRUE section of the IF. For me it didn't changed the context at all but it seems php does likes this more Here is the working code, for anyone with the same problem: $result1 = mysqli_query($con,"SELECT COUNT(*) FROM kunde WHERE email = '$email'"); $select1 = mysqli_fetch_row($result1); $result2 = mysqli_query($con,"SELECT COUNT(*) FROM kunde WHERE adresse = '$adresse'"); $select2 = mysqli_fetch_row($result2); if($select1[0] != 0 && $select2[0] != 0) { } else { $sqlkunde ="INSERT INTO kunde (kunde_geschlecht, vorname, nachname, email, adresse) VALUES ('$geschlecht', '$vorname', '$nachname', '$email', '$adresse')"; mysqli_query($con,$sqlkunde); } Thanks again. This Forum is awsome Edited March 12, 2014 by PAGO Quote Link to comment https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/#findComment-1472394 Share on other sites More sharing options...
Ch0cu3r Posted March 13, 2014 Share Posted March 13, 2014 Why are you writing "$select1[0]", what does the 0 stand for? I always thought, that i have to entere the column here. Because I am using mysql_fetch_row, not mysql_fetch_assoc I changed the = to != and placed the INSERT in the ELSE section. My bad, != should of been == if($select1[0] == 0 && $select2[0] == 0) { // do insert } Quote Link to comment https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/#findComment-1472434 Share on other sites More sharing options...
PAGO Posted March 13, 2014 Author Share Posted March 13, 2014 Yes wrong operator. <- stupido Thanks Quote Link to comment https://forums.phpfreaks.com/topic/286923-select-in-an-if-statement/#findComment-1472437 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.