Adrienk Posted December 10, 2010 Share Posted December 10, 2010 I have <?php /** This file is responsible for processing all the add and modify requests for the movies, games and customer section of the admin page. The various functions here can be called into the pages we need inorder to make code management cleaner and easier to handle. **/ include('databaseconfig.php'); //Add a new customer to the data base. $query = mysql_query("update customer set customer_id = '" .$_POST['cust_id'] . "', first_name = '" .$_POST['fname'] . "', last_name = '" .$_POST['lastname'] . "', adress = '" .$_POST['address'] . "', postal_code= '" .$_POST['postalcode'] . "', phone_number= '" .$_POST['phonenumber'] . "', user_name = '" .$_POST['fname'] . "', user_pass = '" .$_POST['lastname'] . "'"); echo("<h1>Output</h1>"); echo($query); echo("<h1>Possible Errors</h1>"); //Echo errors echo mysql_errno($query) . ": " . mysql_errno($query); ?> Which should get the information from the forum feilds and insert that into the data base, throwing errors if I left feilds null. But when I run it and leave everything blank and hit submit or even actually enter the required data all I get is a page that states Output Possible Errors : Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2010 Share Posted December 11, 2010 Are you sure you mean to be using an UPDATE query, rather than an INSERT? Typically an UPDATE query would have a WHERE clause to avoid updating every record in the database with the same values. Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 11, 2010 Author Share Posted December 11, 2010 Ah yes thank you, But alas I have a new issue The code never executes and the page just refreshes with the "else" out put of "Possible errors :" out put. I have pasted the entire page below, I should have used pastebin, but I have noticed on some forums people will NOT go away from the site in fear of viruses. So basically all you do it hit submit and the code never executes..... The page just refreshes with an empty forum. I am not liking how this forum "formats" php so from now on if I have to post code I am posting it on paste bin. <?php session_start(); include('CheckLogin.php'); include('databaseconfig.php'); $query=mysql_query("insert into customer(cust_id, first_name, last_name, adress, postal_code, phone_number,user_name,user_pass) values('" .$_POST['cust_id'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ','" .$_POST['address'] ." ', '" .$_POST['postalcode'] ." ','" .$_POST['phonenumber'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ')"); ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Home </title> <script type="text/javascript" src="javaScript/jquery-min.js"></script> <script type="text/javascript" src="javaScript/corners.js"></script> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> </script> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <header> <div id="header"> <h1 id="logo">RAY AND CARLS </h1> <p class="description"> <?php echo checkAdmin();?> <br/> </p> </div> </header> <nav> <ul id="nav"> <li> <a href="index.php">Home </a> </li> <li> <a href="#">Customer Registration </a> </li> <li> <a href="#">View Movies </a> </li> <li> <a href="#">View Games </a> </li> <li> <a href="#">Rent </a> </li> <li> <a href="#">Login </a> </li> <li> <a href="#">Admin </a> </li> </ul> </nav> <article> <section> <div class="cornerBox cBox3"> <div class="cornerBox-content"> <p>All updates to the Data base can be done here. Keeping in mind specific relationships with The back end data bases. </p> <p>Some common tasks are below. This page allows you to add, edit and delete customers/movies and games. </p> <div class="cornerBox2 cBox3"> <div class="cornerBox-content"> <h1>Menu </h1> <ul> <h2>Customers </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> <h2>Movies </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> <h2>Games </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> </ul> </div> </div> <div class="Forum"> <h2>Add New Customer </h2> <form class="cmxform" id="commentForm" method="post" action=""> <p> <label for="cname">Customer ID: </label> <em>* </em> <input id="cname" name="cust_id" size="25" class="required" minlength="2"/> </p> <p> <label for="cemail">First Name </label> <em>* </em> <input id="cemail" name="fname" size="25" class="required email"/> </p> <p> <label for="curl">Last Name </label> <em>* </em> <input id="curl" name="lastname" size="25" class="url" value=""/> </p> <p> <label for="curl">Address </label> <em>* </em> <input id="curl" name="address" size="25" class="url" value=""/> </p> <p> <label for="curl">Postal Code </label> <em>* </em> <input id="curl" name="postalcode" size="7" class="url" value=""/> </p> <p> <label for="curl">Phone Number </label> <em>* </em> <input id="curl" name="phonenumber" size="11" class="url" value=""/> </p> <p> <input class="submit" type="submit" value="Submit"/> </p> </form> <?php if (!mysql_query($query)) { echo("<h1>Possible Errors </h1>"); //Echo errors echo mysql_errno($query) . ": " . mysql_errno($query); echo (" <p>"); } else { echo(" <h1>Output </h1>"); echo($query); } ?> </div> </div> </div> </section> </article> <footer> <div id="footer"> <div class="footer1"> <h2> About </h2> <p> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. </p> </div> <div class="footer2"> <h2>Other places </h2> <p> <ul> <li> <a href="#">Link 1 </a> </li> <li> <a href="#">Link 2 </a> </li> <li> <a href="#">Link 3 </a> </li> <li> <a href="#">Link 4 </a> </li> </ul> </p> </div> <div class="footer3"> <h2> Other information </h2> <p> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. </p> </div> <p class="credits">© <a href="http://bwshome.byethost5.com/blogs">Adam Balan - BLACKANDWHITE </a> <span>• </span> Powered by <a href="http://bwshome.byethost5.com/blogs">BLACKANDWHITE </a> <span>• </span> </div> </footer> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
OOP Posted December 11, 2010 Share Posted December 11, 2010 You need to set the action attribute of your form! Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 11, 2010 Share Posted December 11, 2010 You need to set the action attribute of your form! <form class="cmxform" id="commentForm" method="post" action=""> ... it does seem like he did that! when it is empty (which is totally fine html and will be verified with validators), it will just go to the page you are currently on. unless you mean he should direct the data to another page, i dont think adding something to the action form will do a difference... Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 11, 2010 Author Share Posted December 11, 2010 I thought action="" auto refreshes the page after executing the query based on the submit button..... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2010 Share Posted December 11, 2010 You have no logic in place to keep the query from executing if the form hasn't actually been submitted. $query holds a query result resource (if it executed) so in: if( !mysql_query($query) ), you attempt to execute a query using a query result resource instead of a query string. You use mysql_errno() twice. Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 11, 2010 Author Share Posted December 11, 2010 Ok sure there are some logic issues But how do I get it to A Fire only when submit and B catch and spit out any MYSQL errors? Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 11, 2010 Share Posted December 11, 2010 as Pikachu2000 says, check if the form has been submitted. if it has, then you can do the mysql stuff. to see the mysql error: mysql_query($query) or die(mysql_error()); to check if it went fine put it like this: if(mysql_query($query)){ echo 'success'; } Quote Link to comment Share on other sites More sharing options...
OOP Posted December 11, 2010 Share Posted December 11, 2010 Sorry I did not see any PHP code and I thought he has the logic in another php file. That is why i asked him to set the action to the required file. Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 11, 2010 Author Share Posted December 11, 2010 Ok So I did some digging around and shifted some code to make this: <?php session_start(); include('CheckLogin.php'); include('databaseconfig.php'); if (isset($_POST['submit'])) { mysql_query("insert into customer(cust_id, first_name, last_name, adress, postal_code, phone_number,user_name,user_pass) values('" .$_POST['cust_id'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ','" .$_POST['address'] ." ', '" .$_POST[ 'postalcode'] ." ','" .$_POST['phonenumber'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ')"); } ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Home </title> <script type="text/javascript" src="javaScript/jquery-min.js"></script> <script type="text/javascript" src="javaScript/corners.js"></script> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> </script> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <header> <div id="header"> <h1 id="logo">RAY AND CARLS </h1> <p class="description"> <?php echo checkAdmin();?> <br/> </p> </div> </header> <nav> <ul id="nav"> <li> <a href="index.php">Home </a> </li> <li> <a href="#">Customer Registration </a> </li> <li> <a href="#">View Movies </a> </li> <li> <a href="#">View Games </a> </li> <li> <a href="#">Rent </a> </li> <li> <a href="#">Login </a> </li> <li> <a href="#">Admin </a> </li> </ul> </nav> <article> <section> <div class="cornerBox cBox3"> <div class="cornerBox-content"> <p>All updates to the Data base can be done here. Keeping in mind specific relationships with The back end data bases. </p> <p>Some common tasks are below. This page allows you to add, edit and delete customers/movies and games. </p> <div class="cornerBox2 cBox3"> <div class="cornerBox-content"> <h1>Menu </h1> <ul> <h2>Customers </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> <h2>Movies </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> <h2>Games </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> </ul> </div> </div> <div class="Forum"> <h2>Add New Customer </h2> <form class="cmxform" id="commentForm" method="post" action=""> <p> <label for="cname">Customer ID: </label> <em>* </em> <input id="cname" name="cust_id" size="25" class="required" minlength="2"/> </p> <p> <label for="cemail">First Name </label> <em>* </em> <input id="cemail" name="fname" size="25" class="required email"/> </p> <p> <label for="curl">Last Name </label> <em>* </em> <input id="curl" name="lastname" size="25" class="url" value=""/> </p> <p> <label for="curl">Address </label> <em>* </em> <input id="curl" name="address" size="25" class="url" value=""/> </p> <p> <label for="curl">Postal Code </label> <em>* </em> <input id="curl" name="postalcode" size="7" class="url" value=""/> </p> <p> <label for="curl">Phone Number </label> <em>* </em> <input id="curl" name="phonenumber" size="11" class="url" value=""/> </p> <p> <input class="submit" type="submit" value="Submit"/> </p> </form> <?php if (!mysql_query($query)) { echo("<h1>Possible Errors </h1>"); //Echo errors echo mysql_error(); echo ("<p>"); } else { echo("<h2>Customer was added</h1>"); } ?> </div> </div> </div> </section> </article> <footer> <div id="footer"> <div class="footer1"> <h2> About </h2> <p> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. </p> </div> <div class="footer2"> <h2>Other places </h2> <p> <ul> <li> <a href="#">Link 1 </a> </li> <li> <a href="#">Link 2 </a> </li> <li> <a href="#">Link 3 </a> </li> <li> <a href="#">Link 4 </a> </li> </ul> </p> </div> <div class="footer3"> <h2> Other information </h2> <p> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. </p> </div> <p class="credits">© <a href="http://bwshome.byethost5.com/blogs">Adam Balan - BLACKANDWHITE </a> <span>• </span> Powered by <a href="http://bwshome.byethost5.com/blogs">BLACKANDWHITE </a> <span>• </span> </div> </footer> </div> </body> </html> My issue is as follows: 1 it auto executes, so under the forum I get "possible errors: query was/is empty" 2 even after filling in the forum data the else statement never executes stating "customer was added" and the page just refreshes with an empty forum (which is fine) but with the whole possible error thing. Why is it auto executing? - I guess its from my if statement? Why is it not inserting? - I can retrieve stuff from the data base so that's not the issue am I doing something wrong in the code? By the way, thanks all for the patience and the help you've been giving, lots of other forums are like "you suck get out" I am new to PHP. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 11, 2010 Share Posted December 11, 2010 I am not liking how this forum "formats" php so from now on if I have to post code I am posting it on paste bin. Doesn't matter whether you like it. Matters whether we do Right, first, the code will NOT auto execute that query after your first if. The only way the code within that if will be executed is if a form is submitted with that variable $_POST['submit']. We need to see your checklogin and databaseconfig files to be sure. The insert will only work upon form submission. Not sure those gaps in your code are doing it any good (in the insert query). There must be something in your code which is making the forum display it in such a way. Likely a tab, which could affect the query. Also, the query is being run directly and not via a $query variable so when you check for the query lower in your code, you're not checking the insert query at all. Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 12, 2010 Author Share Posted December 12, 2010 So how would you guys take the forum input and make it into a insert query in php? I know this is basically "please give me the code" but I am running out of ideas, and not understanding what I am doing. Checklogin just checks if the user is logged in, if not - return you to the login page. databaseconfig just connects you to the data base. there auto run scripts. Quote Link to comment Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 Hi, Well I have noticed two things: First, you need to add a name attribute to you submit. So change the below: <input class="submit" type="submit" value="Submit"/> to <input class="submit" type="submit" value="Submit" name="submit" /> Also, you need to do some cleanup. I have put all PHP code together at the top of your script [code=php:0] <?php session_start(); include('CheckLogin.php'); include('databaseconfig.php'); if (isset($_POST['submit'])) { $result = mysql_query("insert into customer(cust_id, first_name, last_name, adress, postal_code, phone_number,user_name,user_pass) values('" .$_POST['cust_id'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ','" .$_POST['address'] ." ', '" .$_POST[ 'postalcode'] ." ','" .$_POST['phonenumber'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ')"); if (!$result) { echo("<h1>Possible Errors</h1>"); echo mysql_error(); echo ("<p>"); } else { echo("<h2>Customer was added</h1>"); } } ?> [/code] The rest of html should be the same. no changes Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 12, 2010 Author Share Posted December 12, 2010 Thanks but by moving that echo statement you now make it echo at the top of the page and not where I had it before Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 12, 2010 Author Share Posted December 12, 2010 Sorry for the double post, but adding the name submit doesnt change the outcome of the statement never getting executed Quote Link to comment Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 You need to display the message once you have a problem while inserting the data, right? Quote Link to comment Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 can you please post you code after modification? Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 12, 2010 Author Share Posted December 12, 2010 Ok so i made the modification to the code. But How will this display "sucess" or the error BELOW the forum after hitting submit? It wont, in my experience. but this whole thread is about why will this insert statement not work when I hit submit? <?php session_start(); include('CheckLogin.php'); include('databaseconfig.php'); if (isset($_POST['submit'])) { $resault = mysql_query("insert into customer(cust_id, first_name, last_name, adress, postal_code, phone_number,user_name,user_pass) values('" .$_POST['cust_id'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ','" .$_POST['address'] ." ', '" .$_POST[ 'postalcode'] ." ','" .$_POST['phonenumber'] ." ','" .$_POST['fname'] ." ','" .$_POST['lastname'] ." ')"); if(!$resault) { echo mysql_error(); } else { echo "<p>successfully added</p>"; } } ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Home </title> <script type="text/javascript" src="javaScript/jquery-min.js"></script> <script type="text/javascript" src="javaScript/corners.js"></script> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> </script> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <header> <div id="header"> <h1 id="logo">RAY AND CARLS </h1> <p class="description"> <?php echo checkAdmin();?> <br/> </p> </div> </header> <nav> <ul id="nav"> <li> <a href="index.php">Home </a> </li> <li> <a href="#">Customer Registration </a> </li> <li> <a href="#">View Movies </a> </li> <li> <a href="#">View Games </a> </li> <li> <a href="#">Rent </a> </li> <li> <a href="#">Login </a> </li> <li> <a href="#">Admin </a> </li> </ul> </nav> <article> <section> <div class="cornerBox cBox3"> <div class="cornerBox-content"> <p>All updates to the Data base can be done here. Keeping in mind specific relationships with The back end data bases. </p> <p>Some common tasks are below. This page allows you to add, edit and delete customers/movies and games. </p> <div class="cornerBox2 cBox3"> <div class="cornerBox-content"> <h1>Menu </h1> <ul> <h2>Customers </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> <h2>Movies </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> <h2>Games </h2> <li> <a href="#">edit </a> </li> <li> <a href="#">Add New </a> </li> </ul> </div> </div> <div class="Forum"> <h2>Add New Customer </h2> <form class="cmxform" id="commentForm" method="post" action=""> <p> <label for="cname">Customer ID: </label> <em>* </em> <input id="cname" name="cust_id" size="25" class="required" minlength="2"/> </p> <p> <label for="cemail">First Name </label> <em>* </em> <input id="cemail" name="fname" size="25" class="required email"/> </p> <p> <label for="curl">Last Name </label> <em>* </em> <input id="curl" name="lastname" size="25" class="url" value=""/> </p> <p> <label for="curl">Address </label> <em>* </em> <input id="curl" name="address" size="25" class="url" value=""/> </p> <p> <label for="curl">Postal Code </label> <em>* </em> <input id="curl" name="postalcode" size="7" class="url" value=""/> </p> <p> <label for="curl">Phone Number </label> <em>* </em> <input id="curl" name="phonenumber" size="11" class="url" value=""/> </p> <p> <input class="submit" type="submit" value="Submit" name="submit"/> </p> </form> </div> </div> </div> </section> </article> <footer> <div id="footer"> <div class="footer1"> <h2> About </h2> <p> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. </p> </div> <div class="footer2"> <h2>Other places </h2> <p> <ul> <li> <a href="#">Link 1 </a> </li> <li> <a href="#">Link 2 </a> </li> <li> <a href="#">Link 3 </a> </li> <li> <a href="#">Link 4 </a> </li> </ul> </p> </div> <div class="footer3"> <h2> Other information </h2> <p> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. </p> </div> <p class="credits">© <a href="http://bwshome.byethost5.com/blogs">Adam Balan - BLACKANDWHITE </a> <span>• </span> Powered by <a href="http://bwshome.byethost5.com/blogs">BLACKANDWHITE </a> <span>• </span> </div> </footer> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 assign the query string to a variable and echo it before using it. you will see what is missing, probably some variable that isn't set or some typo! Quote Link to comment Share on other sites More sharing options...
Adrienk Posted December 12, 2010 Author Share Posted December 12, 2010 It all works now, thank you all for your help. I have other questions on other scripts Should I post those here or make a new thread? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 Create a new thread since it's a different script. Quote Link to comment 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.