aztec Posted February 8, 2008 Share Posted February 8, 2008 Hello I am having a problem getting multiple fields posted from a form using PHP into a MYSQL database. The following code will only allow me to input the first field, ie first_name. <?php if (isset($_POST['first_name'])): $dbcnx = @mysql_connect('xxxxxx', 'yyyyyy', 'zzzzzz'); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('xxxxxx')) { exit('<p>Unable to locate the xxxxxx ' . 'database at this time.</p>'); } $first_name = $_POST['first_name']; $second_name = $_POST['second_name']; $surname = $_POST['surname']; $sql = "INSERT INTO QQQQQQ SET first_name='$first_name', second_name='$second_name', surname = '$surname'”; <?php else: // Allow the user to enter a new person ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" > <table> <tr> <!--<label>First Name:<input type="varchar" name="first_name" /></label><br />--> <td colspan="2" class="lablecell"><label>First Name</label></td> <td colspan="2" class="fieldcell"> <input type="varchar" name="first_name" /></td> </tr> <tr> <td colspan="2" class="lablecell"><label>Second Name</label></td> <td colspan="2" class="fieldcell"> <input type="varchar" second_name="second_name" /></td> </tr> <tr> <td colspan="2" class="lablecell"><label>Surname</label></td> <td colspan="2" class="fieldcell"> <input type="varchar" surname="surname" /></td> </tr> <tr> <tr> <td colspan="2"><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> <?php endif; ?> Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/ Share on other sites More sharing options...
aztec Posted February 8, 2008 Author Share Posted February 8, 2008 Hello Sorry the info is cryptic but I was having problems getting a preview. The form is styled using an external CCS script. I have been trying to get by this problem for some time now, I think it needs a new pair of eyes to spot the error. Kind Regards Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/#findComment-461892 Share on other sites More sharing options...
rcorlew Posted February 8, 2008 Share Posted February 8, 2008 To insert new data row into mysql use this query instead: $query = "INSERT INTO my_table (first_name, last_name) VALUES ('$first_name', '$last_name')"; $res = mysql_query($query) or die; Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/#findComment-461977 Share on other sites More sharing options...
Barand Posted February 8, 2008 Share Posted February 8, 2008 Your form is wrong. The fields should be <input type="text" name="first_name" /> <input type="text" name="second_name" /> <input type="text" name="surname" /> Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/#findComment-461986 Share on other sites More sharing options...
aztec Posted February 8, 2008 Author Share Posted February 8, 2008 Hello Berand I changed the field type from varchar to text but when I submit the form only the first field was entered into the DB Regards Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/#findComment-462001 Share on other sites More sharing options...
Barand Posted February 8, 2008 Share Posted February 8, 2008 The important bit is that they should all have a name attribute <input type="text" name="first_name" /> <input type="text" name="second_name" /> <input type="text" name="surname" /> Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/#findComment-462007 Share on other sites More sharing options...
aztec Posted February 8, 2008 Author Share Posted February 8, 2008 Hello Barand I thought that each field type had to match the field type in the database, how wrong I was. Thanks very much for setting me right, I changed everything to name and all 23 fields on the actual database were input correctly. The problem is now solved Kind Regards Link to comment https://forums.phpfreaks.com/topic/90087-solved-posting-multiple-fields-using-php/#findComment-462026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.