argan328 Posted March 19, 2007 Share Posted March 19, 2007 Hi Need help posting an array to my mysql table (using mysql version 4.0). I have an array: $param = array( 'calleridname' => $_POST['calleridname'], 'callerid' => $_POST['callerid'], 'phonenumber' => $_POST['phonenumber'], 'message' => $_POST['message'], ); and I have a database table with 4 fields calleridname callerid phonenumber message and would like to post the array to those fields in the database. do I use $sql = "INSERT INTO messages SET calleridname='$calleridname', create_date=CURDATE() callerid='$callerid', phonenumber='$phonenumber', message='$message' "; ???? This doesn't seem to work for me. Do I use a foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/ Share on other sites More sharing options...
btherl Posted March 19, 2007 Share Posted March 19, 2007 Can you post your full code (or at least the entire section related to mysql), and also tell us what happens when you try to store the data? Is there any error message? Is anything stored (even if it isn't what you expected to be stored)? Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-210290 Share on other sites More sharing options...
argan328 Posted March 19, 2007 Author Share Posted March 19, 2007 it seems sometimes I get a mysql error but then other times when I mess around with the code I get no error nor exception thrown, it just doesn't work. Here is the form on my main page which calls Phone.php: <form target="_blank" action="Phone.php" method="post" ENCTYPE="multipart/form-data"> <table style="height: 100%" width="100%" cellspacing="0" class="messageentry" border="1" cellpadding="4" bordercolor="#dcdcdc" rules="none"> <tr> <td colspan="2" align="center"> <h2>Leave your message</h2> </td> </tr> <tr> <td>Your Name:</td> <td><input type="edit" size="25" name="calleridname"/></td> </tr> <tr> <td>Your Phone Number:</td> <td><input type="edit" size="14" name="callerid"/></td> </tr> <tr> <td >Phonenumber:</td> <td><input type="edit" size="14" name="phonenumber"/></td> </tr> <tr> <td>Your Message:</td> <td><textarea id="text" rows="5" cols="24" name="message"/></textarea></td> </tr> <tr> <td align="center" colspan="2" style="border:1px solid #000000"> <br> <input type="submit" name="submit" value="Submit" id="mySubmit" disabled="disabled" /> </td> </tr> </table> </form> ((You may wonder why I need an array to begin with and the answer is I need it for the next step in the site I'm creating)) and here is Phone.php: <?php $param = array( 'calleridname' => $_POST['calleridname'], 'callerid' => $_POST['callerid'], 'phonenumber' => $_POST['phonenumber'], 'message' => $_POST['message'], ); //Connect To Database $hostname="xxxx"; $username="xx_xx"; $password="xxxx"; $dbname="xx_xx"; $usertable="xxxx"; $yourfield = "xxxx"; $dbcnx = @mysql_connect($hostname, $username, $password); if (!$dbcnx) { echo '<p>Unable to connect to the ' . 'database server at this time.</p>'; exit(); } // Select the database if (!@mysql_select_db($dbname)) { exit('<p>Unable to locate the ' . 'database at this time.</p>'); } //implode the string-- I put this in because I thought I may need to use this feature for the array $stringout = implode(",", $param); echo $stringout; // Submit message to the database-- I think this is where my problem is.... if (isset($_POST['$stringout'])) { $stringout = $_POST['$stringout']; $sql = "INSERT INTO messages SET calleridname='$calleridname', create_date=CURDATE() callerid='$callerid', phonenumber='$phonenumber', message='$message' "; if (@mysql_query($sql)) { echo '<p>Your array has been added.</p>'; } else { echo '<p>Error adding submitted message: ' . mysql_error() . '</p>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-210417 Share on other sites More sharing options...
bwochinski Posted March 19, 2007 Share Posted March 19, 2007 That isn't how you form an INSERT query... $sql = "INSERT INTO messages (calleridname, create_date, callerid, phonenumber, message) VALUES ('$calleridname', CURDATE(), '$callerid', '$phonenumber', '$message')"; Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-210799 Share on other sites More sharing options...
Barand Posted March 20, 2007 Share Posted March 20, 2007 @bwochinski See second syntax option here http://dev.mysql.com/doc/refman/4.1/en/insert.html Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-210847 Share on other sites More sharing options...
btherl Posted March 20, 2007 Share Posted March 20, 2007 Oh.. is the problem that there is no comma after create_date=CURDATE() ? If so, mysql_error() should have given you additional information about the error. Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-210881 Share on other sites More sharing options...
argan328 Posted March 21, 2007 Author Share Posted March 21, 2007 thanks all for your input, without the comma after create_date=CURDATE() I get mysql error Error adding submitted message: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'callerid='', phonenumber='', message=''' at line 4 when I put the comma in I get this error Error adding submitted message: Duplicate entry '' for key 1 Do you know what this error means? Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212433 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 It means you tried to add a record with '' in whichever column is defined as unique and there is already a record with that value in that column. Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212438 Share on other sites More sharing options...
argan328 Posted March 22, 2007 Author Share Posted March 22, 2007 Ok Barand I'm making progress... I finally have received the message "Your array has been added" and I jumped for joy, then I went to the database screen to see my results and the only field that was populated was the create_date... all the others were blank. Is there something in my code that would cause this? Thanks again in advance! Argan Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212458 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 How is the table defined? Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212469 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 Lose the $param array and just have $calleridname = $_POST['calleridname']; $callerid = $_POST['callerid']; $phonenumber = $_POST['phonenumber']; $message = $_POST['message']; Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212470 Share on other sites More sharing options...
argan328 Posted March 22, 2007 Author Share Posted March 22, 2007 the thing is I have to have the $param array b/c of where I'm going next with the page (the vendor I'll be sending the info to will be looking for the $param array) - is it possible to have the $param array defined at the top and then down below when I'm inputting into the table spell it out like you've just indicated or will that screw things up? Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212474 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 OK If you keep the array $sql = "INSERT INTO messages SET calleridname='{$param['calleridname']}', create_date=CURDATE(), callerid='{$param['calleridid']}', phonenumber='{$param['phonenumber']}', message='{$param['message']}' "; Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212476 Share on other sites More sharing options...
argan328 Posted March 22, 2007 Author Share Posted March 22, 2007 now I'm getting no response, neither of my echo's are showing up... I think my $stringout in the if statement may be messing things up. if you recall from above I thought I was going to have to implode the array so I created $stringout and plopped it in the if statement. I know the problem is somewhere in the code below... do you know why I would not get either of my echo statements? sorry to be such a pain on this one but I'm coming up on my deadline! :-\ //implode the string $stringout = implode(",", $param); echo $stringout; // Submit message to the database if (isset($_POST['$stringout'])) { $stringout = $_POST['$stringout']; $sql = "INSERT INTO messages SET calleridname='{$param['calleridname']}', create_date=CURDATE(), callerid='{$param['calleridid']}', phonenumber='{$param['phonenumber']}', message='{$param['message']}' "; if (@mysql_query($sql)) { echo '<p>Your array has been added.</p>'; //neither of these are showing up now?? } else { echo '<p>Error adding submitted message: ' . mysql_error() . '</p>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-212522 Share on other sites More sharing options...
argan328 Posted March 23, 2007 Author Share Posted March 23, 2007 can anyone tell me why neither of my echos would be showing up? Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-213171 Share on other sites More sharing options...
Barand Posted March 23, 2007 Share Posted March 23, 2007 When you echo $stringout, check your form and see if you have a form field whouse name matches "stringout value. You will only output anything if there is because of this if (isset($_POST['$stringout'])) { Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-213183 Share on other sites More sharing options...
fenway Posted March 23, 2007 Share Posted March 23, 2007 Must be a PHP syntax error somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/43311-post-an-array-to-table/#findComment-213436 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.