driz Posted March 26, 2009 Share Posted March 26, 2009 Hello, please take a look at the following code: <?php $dog_name=$_POST['dog_name']; $dog_gender=$_POST['dog_gender']; $dog_age=$_POST['dog_age']; $dog_breed=$_POST['dog_breed']; $dog_with_children=$_POST['dog_with_children']; $dog_with_dogs=$_POST['dog_with_dogs']; $dog_description=$_POST['dog_description']; $dog_photo=$_POST['dog_photo']; $shelter_id=$_POST['shelter_id']; $submit=$_POST['submit']; if($submit!="submit") { $host='localhost'; $username='u0558234'; $pass='26jun87'; mysql_connect($host,$username,$pass); mysql_select_db($username); $query="insert into dogs values (NULL,'".$dog_name."','".$dog_gender."','".$dog_age."','".$dog_breed."','".$dog_with_children."','".$dog_with_dogs."','".$dog_description."','".$dog_photo."','".$shelter_id."')"; $result=mysql_query($query); /*if(mysql_affected_rows()==1){ echo 'details were successfully added'; } else { echo 'there was a problem'; }*/ mysql_close(); };?><?php require('header.php'); ?> <!--CONTENT--> <div id="content"> <div class="section"> <form action="admin_add_dog.php" method="post"> <fieldset> <legend>Add Dog</legend> <ul> <li> <label><strong>Name:</strong></label> <input type="text" name="dog_name" /> </li> <li> <label><strong>Gender:</strong></label> <select name="dog_gender"> <option value="Male">Male</option> <option value="Female">Female</option> </select> </li> <li> <label><strong>Age:</strong></label> <input type="text" name"dog_age" /> </li> <li> <label><strong>Breed:</strong></label> <input type="text" name"dog_breed" /> </li> <li> <label><strong>Home with Children:</strong></label> <input type="radio" name="dog_with_children" checked="checked" value="Yes" /> <input type="radio" name="dog_with_children" value="No" /> </li> <li> <label><strong>Home with other Dogs:</strong></label> <input type="radio" name="dog_with_dogs" checked="checked" value="Yes" /> <input type="radio" name="dog_with_dogs" value="No" /> </li> <li> <label><strong>Description:</strong></label> <textarea name="dog_description"></textarea> </li> <li> <label><strong>Photo:</strong></label> <input type="text" name"dog_photo" /> </li> <li> <label><strong>Shelter:</strong></label> <select name="shelter_id"> <option value="1">Pennine Animal Shelter</option> <option value="2">All Pets Rescue Centre</option> <option value="3">New Hope Dog Shelter</option> <option value="4">The Old Mill Rescue Centre</option> <option value="5">4 Paws Welfare Centre</option> </select> </li> <li> <input type="submit" value='submit' name='submit'> </li> <ul> </fieldset> </form> </div> </div> <!--/CONTENT--> <?php require('sidebar.php'); ?> <?php require('footer.php'); ?> This is the code for a page called admin_add_dog.php and when submitted should add the submitted data to a table called dogs in my database!However it isn't adding the data, can you see any errors or reason why this isn't working? Thanks. Link to comment Share on other sites More sharing options...
jeffman Posted March 26, 2009 Share Posted March 26, 2009 echo mysql_error(); Link to comment Share on other sites More sharing options...
sooty2006 Posted March 26, 2009 Share Posted March 26, 2009 replace php sumbit with this, dont forget to rename the attributes to were the infomation will be stored! on INSERT. <?php $dog_name=$_POST['dog_name']; $dog_gender=$_POST['dog_gender']; $dog_age=$_POST['dog_age']; $dog_breed=$_POST['dog_breed']; $dog_with_children=$_POST['dog_with_children']; $dog_with_dogs=$_POST['dog_with_dogs']; $dog_description=$_POST['dog_description']; $dog_photo=$_POST['dog_photo']; $shelter_id=$_POST['shelter_id']; $submit=$_POST['submit']; if($submit!="submit") { $host='localhost'; $username='u0558234'; $pass='26jun87'; mysql_connect($host,$username,$pass); mysql_select_db($username);mysql_query("INSERT INTO `dogs values` ( `id` , `name` , `gender` , `age` , `breed` , `with_child` , `with_dog` , `description` ,`photo` , `shelter` ) VALUES (NULL, '$dog_name', '$dog_gender, '$dog_age', '$dog_breed', '$dog_with_children', '$dog_with_dogs', 'dog_description', '$dog_photo', '$shelter_id' )"); echo 'details were successfully added'; }else{ echo 'there was a problem'; } mysql_close(); };?> Link to comment Share on other sites More sharing options...
driz Posted March 26, 2009 Author Share Posted March 26, 2009 The error was: Cannot add or update a child row: a foreign key constraint fails!That has been fixed now!But when submitting the form, the fields : age, breed, and photo were blank, even though text was entered into the fields. Is their an error somewhere in my code that is making that happen? I've also noticed that it creates two rows, a complete blank one, and then the one I want but the missing data mentioned?? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 26, 2009 Share Posted March 26, 2009 Print the query out to see if the values are in the query. If they aren't check the names of everything to make sure you're using the right names. If you see the values in the query but they don't get saved correctly in the database, check the data types in the table to make sure it can handle the data you're sending. Link to comment Share on other sites More sharing options...
driz Posted March 26, 2009 Author Share Posted March 26, 2009 The problem was that the input fields were coded wrong, I missed the '=' and had them saying name"dog_breed" instead of name="dog_breed" Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.