dsaba Posted March 9, 2006 Share Posted March 9, 2006 i'm having problems with this script of mine, this code that I will paste below is the forminsert.php, the processing php page of my simple html form, i'm trying to insert data into a database with if and else statements pertaining to checking if data is already in the database alreadyi believe the if and else statement and the sql sytanx i used for trying to query more than one table (relational database) is the root of the problem, but basically this is the error message, rather success statement i get: ---------------------------------You have succesfully submitted a map into the database! With your help XboxStuff.org is becoming the largest Farcry Map database known to man!-------------------------------------------------------this is a success statement, yet when I go into php admin to see if data has been added nothing shows, i've refreshed and reloaded several timesbelow is the a look at the script i'm using for the forminsert.php page, again take a look at that if and else statement where i try to insert into more than one table..i believe thats where i go wrong, but not sure how to fix it-----------------------------------------------------------------------------------$sname = $_POST['sname'];$email = $_POST['email'];$mname = $_POST['mname']; $mauthor = $_POST['mauthor'];$chaos = $_POST['chaos'];$sts = $_POST['sts'];$predator = $_POST['predator'];$mtemplate = $_POST['mtemplate'];$mbrushset = $_POST['mbrushset'];$mdescription = $_POST['email'];$mcomments = $_POST['mcomments'];$mupdates = $_POST['mupdates'];$mnotify = $_POST['mnotify'];$uploadedfile = $_POST['uploadedfile'];$date = date("Y-m-d");$datetime = date("Y-m-d g:i:s");$userip = $_SERVER['REMOTE_ADDR'];//if $mname is not defined in db then create a new recordif($mname){ $query = "UPDATE `maps` SET `map_author`='$mauthor', `map_description`='$mdescription', `mapmode_chaos`='$chaos', `mapmode_sts`='$sts', `mapmode_predator`='$predator', `map_template`='$mtemplate', `map_brushset`='$mbrushset', `map_dateadded`='$date', `map_email`='$email', `map_ip`='$userip', `map_submitter`='$sname' WHERE `map_name`='$mname'"; "UPDATE `comments` SET `comment_text`='$mcomments', `comment_dateadded`='$datetime', `comment_name`='$sname', `comment_ip`='$userip' WHERE `map_name`='$mname'";}else{ $query = "INSERT INTO `maps` ( `map_name`, `map_author`, `map_description`, `mapmode_chaos`, `mapmode_sts`, `mapmode_predator`, `map_template`, `map_brushset`, `map_dateadded`, `map_email`, `map_ip`, `map_submitter` ) VALUES ( '$mname','$mauthor','$mdescription','$chaos','$sts','$predator','$mtemplate','$mbrushset','$date','$email','$userip','$sname' )"; "INSERT INTO `comments` ( `map_name`, `comment_dateadded`, `comment_name`, `comment_text`, `comment_ip` ) VALUES ( '$mname', '$datetime', '$sname', '$mcomments', '$userip' )";}//data is now in the database$results = mysql_query( $query );//success statement based on resultsif( $results ){ echo( "You have succesfully submitted a map into the database! With your help XboxStuff.org is becoming the largest Farcry Map database known to man!");}else{ die( "There was a problem with the file you uploaded or the data you submitted. Try resubmitting again, if problem persists post an error ticket here" . mysql_error() );} ?>-----------------------------------------------------------------------------------------------the purpose of the if and else statement is to find out if a field of "map_name" is already in the database, if so then the data will simply update the record, if not then the data will be a new record with the new "map_name" from the form its named "$mname"thanks in advance people! Quote Link to comment https://forums.phpfreaks.com/topic/4505-proper-syntax-for-if-else-statements-with-queries-on-mutiple-tables/ Share on other sites More sharing options...
wickning1 Posted March 9, 2006 Share Posted March 9, 2006 You can't just throw two queries together the way you are doing, you're going to have to call mysql_query() twice. You're not even concatenating them both into $query correctly, that's just bad PHP.[code]if($mname){$query = "UPDATE `maps` SET `map_author`='$mauthor', `map_description`='$mdescription',`mapmode_chaos`='$chaos', `mapmode_sts`='$sts', `mapmode_predator`='$predator', `map_template`='$mtemplate', `map_brushset`='$mbrushset', `map_dateadded`='$date', `map_email`='$email', `map_ip`='$userip', `map_submitter`='$sname' WHERE `map_name`='$mname'";$query2 = "UPDATE `comments` SET `comment_text`='$mcomments', `comment_dateadded`='$datetime', `comment_name`='$sname', `comment_ip`='$userip' WHERE `map_name`='$mname'";}else{$query = "INSERT INTO `maps` ( `map_name`, `map_author`, `map_description`, `mapmode_chaos`, `mapmode_sts`, `mapmode_predator`, `map_template`, `map_brushset`, `map_dateadded`, `map_email`, `map_ip`, `map_submitter` )VALUES ( '$mname','$mauthor','$mdescription','$chaos','$sts','$predator','$mtemplate','$mbrushset','$date','$email','$userip','$sname' )";$query2 = "INSERT INTO `comments` ( `map_name`, `comment_dateadded`, `comment_name`, `comment_text`, `comment_ip` )VALUES ( '$mname', '$datetime', '$sname', '$mcomments', '$userip' )";}//data is now in the database$results = mysql_query( $query );$results2 = mysql_query( $query2 );//success statement based on resultsif( $results1 && $results2 ){echo( "You have succesfully submitted a map into the database! With your help XboxStuff.org is becoming the largest Farcry Map database known to man!");}else{die( "There was a problem with the file you uploaded or the data you submitted. Try resubmitting again, if problem persists post an error ticket here" . mysql_error() );}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4505-proper-syntax-for-if-else-statements-with-queries-on-mutiple-tables/#findComment-15766 Share on other sites More sharing options...
dsaba Posted March 9, 2006 Author Share Posted March 9, 2006 thanks for the information, thats just what I was looking for on how to write queries, anywhere where I can see additional things related to mysql syntax? like websites, i usually just google search randomly and get results sometimes and then don'tHOWEVER-----------i tried the new and improved script and it still gives me the error message of "try resubmitting again", but it doesn't give me any proper php or mysql error that could hint at what's going wrongi've tried submitting the form with the map name missing and the email address missing and it doesn't seem to give me any problems, except that the map name and email are missing from my database!i realize the if statement is dependent on the map name, so maybe the way in which i wrote it is causing it to totally omit it in the database? i have no ideawhen i submit everything in the form including the map name, nothign happens at allyet, when i submit without the map name, i have a new record but its missing the email which i supplied and the map namemaybe a better way to write this if and else statement? again the purpose of it is to determine if there already exists a record in my database with the same map name, if so then update recordif not, then create a new record with the new map nameyou rock people, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/4505-proper-syntax-for-if-else-statements-with-queries-on-mutiple-tables/#findComment-15778 Share on other sites More sharing options...
wickning1 Posted March 9, 2006 Share Posted March 9, 2006 Yeah, you'll never be able to insert a map with that script.You need to actually check the database for $mname for that first if() statement. What you're doing now is just checking that the user sent a map, which will always be true. What you want to do is find out if what the user sent *matches* something in the database. Then you know whether you should UPDATE OR INSERT.You're constantly trying to UPDATE a map that doesn't exist.As far as reference material, you can't beat mysql.com and php.net. The only other references you'll ever need are tutorials, which are a dime a dozen. There's quite a few here at PHPFreaks, just look around. Quote Link to comment https://forums.phpfreaks.com/topic/4505-proper-syntax-for-if-else-statements-with-queries-on-mutiple-tables/#findComment-15839 Share on other sites More sharing options...
dsaba Posted March 9, 2006 Author Share Posted March 9, 2006 [!--quoteo(post=353315:date=Mar 9 2006, 12:59 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 9 2006, 12:59 PM) [snapback]353315[/snapback][/div][div class=\'quotemain\'][!--quotec--]Yeah, you'll never be able to insert a map with that script.You need to actually check the database for $mname for that first if() statement. What you're doing now is just checking that the user sent a map, which will always be true. What you want to do is find out if what the user sent *matches* something in the database. Then you know whether you should UPDATE OR INSERT.You're constantly trying to UPDATE a map that doesn't exist.As far as reference material, you can't beat mysql.com and php.net. The only other references you'll ever need are tutorials, which are a dime a dozen. There's quite a few here at PHPFreaks, just look around.[/quote]exactly man! you're reading my mind, i realize that that if statement i have is bad, so what do I need to replace it with in order to check my database to see if $mname is already there, and then if not create a new record? i know what I need to do, and so do you, i just don't know how to write in in php/mysql syntaxthanks for the quick reply Quote Link to comment https://forums.phpfreaks.com/topic/4505-proper-syntax-for-if-else-statements-with-queries-on-mutiple-tables/#findComment-15873 Share on other sites More sharing options...
wickning1 Posted March 9, 2006 Share Posted March 9, 2006 $result = mysql_query("SELECT map_name FROM maps WHERE map_name='$mname'");$row = mysql_fetch_assoc($result);if ($row['map_name'] == $mname) {// ... do all the rest of the stuff Quote Link to comment https://forums.phpfreaks.com/topic/4505-proper-syntax-for-if-else-statements-with-queries-on-mutiple-tables/#findComment-15875 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.