sonnieboy Posted February 22, 2017 Share Posted February 22, 2017 <?php global $wpdb; // Database connection $conn = mysqli_connect("localhost","myuser","mypwd","myDB"); if(!$conn) { die('Problem in database connection: ' . mysql_error()); } // Data insertion into database $sql = 'INSERT INTO `myDB`.`myTable` ( `custname`' . ', `address`, `city`, `zip`, `email` )' . ' VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? )'; if( $sth = mysqli_prepare($conn,$sql) ) { mysqli_stmt_bind_param($sth,'sssss' ,$_POST["custname"] ,$_POST["address"] ,$_POST["city"] ,$_POST["zip"] ,$_POST["email"] ); if( mysqli_stmt_execute($sth) ) { echo '<h1>Thank you</h1> <br> Go back to the main page <a href="index.php");'; } else { printf("Error: %s\n",mysqli_stmt_error($sth)); } } else { printf("Error: %s\n",mysqli_connect_error($conn)); } ?> <form action='process.php' method = 'POST'> <input type='hidden' name='custname' value="<?php echo $custname; ?>"> <input type='hidden' name='address' value="<?php echo $address; ?>"> <input type='hidden' name='city' value="<?php echo $city; ?>"> <input type='hidden' name='zip' value="<?php echo zip; ?>"> <input type='hidden' name='email' value="<?php echo $email; ?>"> Greetings gurus, I hope you guys can forgive me for asking this because this is more of confusion than ignorance. I have some form fields as hidden shown above. When I right-click on my PC to VIEW SOURCE, I see the values passed to them. However, when I tried submitting the values from these hidden forms, I am getting an error that "Custname cannot be null". This tells me that this page is passing null values to process.php page. Every browsing I have done so far, suggests that I am doing it correctly and I have spend more than three hours searching for answers. Does anyone know what could be wrong? Your help in pointing me to the right direction is greatly appreciated. Here is process.php Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2017 Share Posted February 22, 2017 Your query has five columns with five parameters - so why nine placeholders for those parameters? Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 22, 2017 Author Share Posted February 22, 2017 Hi Thanks for helping me again. There were a total of 9 fieldnames. I removed 4 before posting but forgot to reduce that number. Sorry about that. The error is on the first fieldname, custname and I am sure that if I remove that, the error goes to the next line. I am sure the issue is with the hidden field values not recognized by process.php for some reason. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2017 Share Posted February 22, 2017 Have you tried doing a var_dump($_POST) to check that the custname isn't null? Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 22, 2017 Author Share Posted February 22, 2017 Yes, I tried that and the value coming out is NULL which is what is confusing me. In my original thread, I indicated that when I VIEW SOURCE on that page that passed the hidden form values to this process.php, I see the values. So, I guess my confusion is why is process.php page not recognizing those values? Every example I have looked at so far, suggests that I am doing it correctly but why it showing NULL value? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted February 22, 2017 Share Posted February 22, 2017 (edited) Yes, I tried that and the value coming out is NULL which is what is confusing me. In my original thread, I indicated that when I VIEW SOURCE on that page that passed the hidden form values to this process.php, I see the values. So, I guess my confusion is why is process.php page not recognizing those values? Every example I have looked at so far, suggests that I am doing it correctly but why it showing NULL value? So if you do the var_dump($_POST) as the very first line of process.php your other hidden fields have values, but custname is NULL? But if you view the source of the html page your value is there? What is the value? Edited February 22, 2017 by taquitosensei Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 22, 2017 Share Posted February 22, 2017 How about showing us the code that produces that error message? Is it possible a case thing? Your message says 'Custname' but your html says 'custname' Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 22, 2017 Author Share Posted February 22, 2017 No, that's not what I said. Sorry if I confused you. I have a page called review.php. When a customer places an order from the order.php, s/he clicks the Review button and is taken to review.php page. On this page, I have the hidden form fields I posted above. I don't want to risk reposting. After reviewing his or her order on review.php page and if everything is ok, s/he clicks to submit his or her order. This is where we are having issues. I expect those values from the hidden form fields to be passed to the process.php. So, what I am saying is that when I VIEW SOURCE in review.php for those hidden form fields, I see the values. However, when I do the var_dump(...), they show up as null and I can't figure out why. Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 22, 2017 Author Share Posted February 22, 2017 (edited) ginerjm, sorry I had hit the post before I saw your post. The code that produced the error is above on my original post. You will see the code below. printf("Error: %s\n",mysqli_stmt_error($sth)); My error always says: Errror: custname cannot be null. No, it is the casing is correct. If I am sure that if I remove that field, the error goes to next field. Let me ask this, is possible that I am having this problem because I am attempting to insert this record in WordPress db? Edited February 22, 2017 by sonnieboy Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2017 Share Posted February 22, 2017 You only show part of the html for the form. What is the whole form code? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 22, 2017 Share Posted February 22, 2017 Yes - where is the rest of the form? I'm thinking you have a duplicate fieldname in your form code. So - the table has a constraint (or multiples) in that these fields cannot be null. Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 22, 2017 Author Share Posted February 22, 2017 (edited) ?php error_reporting(E_ALL); echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>"; if(isset($_POST['custname'])) $custname = $_POST['custname']; if(isset($_POST['zip'])) $zip = $_POST['zip']; if(isset($_POST['city'])) $city = $_POST['city']; if(isset($_POST['email'])) $email = $_POST['email']; $address = $_POST['address']; $rowIDs = $_POST['rowIDs']; $row2IDs = $_POST['row2IDs']; echo $custname .'<br>'; echo $zip .'<br> <hr width=400 align=left>'; $rowIDs = $_POST['rowIDs']; foreach ($rowIDs as $id) { $agentname = $_POST['agentname' . $id]; $agentaddress = $_POST['agentaddress' . $id]; $agentincome = $_POST['agentincome' . $id]; echo 'Name: '. $agentname . '<br />'; echo 'Address: '. $agentaddress . '<br />'; echo 'agentincome: '. $agentincome . '<br /><br>'; } ?> <body> <form action='process.php' method = 'POST'> <input type='text' name='custname' value="<?php echo $custname; ?>"> <input type='text' name='address' value="<?php echo $address; ?>"> <input type='text' name=email value="<?php echo $email; ?>"> <input type='text' name=city value="<?php echo $city; ?>"> <input type='text' name='zip' value="<?php echo $zip; ?>"> <input type='hidden' name='agentname' value="<?php echo $agentname; ?>"> <input type='hidden' name='agentaddress' value="<?php echo $agentaddress; ?>"> <input type='hidden' name='agentincome' value="<?php echo $agentincome; ?>"> <input type="action" type="button" value="Return to data" onclick="history.go(-1);" /> | <input type="submit" value="Submit your form" /> </form> </body> My sincere apology. Unnecessary pressure from powers to be Here it is: I will eventually add agent info. Those are arrays Edited February 22, 2017 by sonnieboy Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 22, 2017 Share Posted February 22, 2017 So what did your debug statement show you? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2017 Share Posted February 22, 2017 Are you sure that is the same form - the hidden fields you originally posted are text fields in that form? Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 23, 2017 Author Share Posted February 23, 2017 (edited) Barand, Yes, the names are the same. The action is to be processed by process.php. I changed it to text so I can see the values. That should be no brainer even for someone like me. That's why I said I am confused because every example I looked up followed same convention as the one I am following, That's also why I am wondering if the fact that I am trying to insert into a WordPress DB might have something to do with it, I have never done something like this in WordPress before. Edited February 23, 2017 by sonnieboy Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted February 23, 2017 Share Posted February 23, 2017 Barand, Yes, the names are the same. The action is to be processed by process.php. I changed it to text so I can see the values. That should be no brainer even for someone like me. That's why I said I am confused because every example I looked up followed same convention as the one I am following, That's also why I am wondering if the fact that I am trying to insert into a WordPress DB might have something to do with it, I have never done something like this in WordPress before. If you see the custname in your (now) text field. Then at the top of process.php the very first line you need to do var_dump($_POST); just to make sure your post data is making it to your process.php page. It sounds like the field(s) in your database can't be NULL and it's receiving a NULL value for at least $custname. Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 23, 2017 Author Share Posted February 23, 2017 Great point indeed. I changed to text to see the values but I could not. Nothing on screen as though they were still hidden. That's why I had to do a VIEW SOURCE to see if I see any values. Now, your point leads me to a very important question which may lead us to the solution and that question is if I can see the values when I do VIEW SOURCE, what does it mean when I can't see the values when I change the text type from hidden to text? This is really baffling. I one of you great minds can help me. My manager is losing confidence that I can get this done and I have been looking at this for a long while now. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted February 23, 2017 Share Posted February 23, 2017 you're missing a couple of quotes around email and city. Not sure if this is the problem or not. <input type='text' name=email value="<?php echo $email; ?>"> <input type='text' name=city value="<?php echo $city; ?>"> Other than that it's hard to tell at this point. Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 23, 2017 Author Share Posted February 23, 2017 (edited) Thanks for your responses. I did F12 to see what's going on and this is what I see: <input�type='text'�name='custname'�value="John Doe"=""> This is why the screen is showing blank. Do you know why that is happening and how to resolve it? I have never seen anything like this before. Edited February 23, 2017 by sonnieboy Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 23, 2017 Share Posted February 23, 2017 You see that input tag line EXACTLY as you typed it here with the little boxes and the unnecessary quotes at the end? Have you tried re-typing that line into your script? Delete the entire line and start it over? Did you copy this code from somewhere that introduced a character set you are not expecting? Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 23, 2017 Author Share Posted February 23, 2017 You see that input tag line EXACTLY as you typed it here with the little boxes and the unnecessary quotes at the end? As you know, sometimes when you look at your screen for extended period of time, you can no longer see anything beyond what you have been seeing. Sometimes, when we post, we do so because we need coding help. Sometimes, it is to get a new set of eyes to look at things. Anyway, it is resolved now. Thanks to everyone who responded. I didn't need to copy those few lines. I must have inadvertently typed in something that created the problem that bugged me down for days. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 23, 2017 Share Posted February 23, 2017 I assume it was that line of html, since you didn't tell use who have been trying to help you. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 23, 2017 Share Posted February 23, 2017 I must have inadvertently typed in something Get a proper IDE or editor. 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.