willhub Posted March 24, 2011 Share Posted March 24, 2011 Hi. For a uni assignment I am turning my static prototype into a dynamic prototype and adding various features like templates, log in system and the ability to add/delete information stored on the database and view the information on the website. I have running into a problem though, I am having difficulties getting connected to the database, I don't know if this is something to do with the log in information though, below I have quoted the files. update.php (the script I use to connect to the database) <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ $name = $_POST['name']; $sname = $_POST['sname']; $address = $_POST['address']; $number = $_POST['number']; mysql_connect("localhost", "", "") or die ('error: ' .mysql_error()); mysql_select_db ("bza410_test"); $query="INSERT INTO testTable (id, firstname, surname, address, phone)VALUES ('NULL','".$name."', '".$sname."', '".$address."', '".$number."')"; mysql_query($query) or die ('error updating database'); echo "database updated with: " .$name. " ".$sname." ".$address." ".$number ; ?> after localhost I am aware password/username goes there, just removed them for obvious reasons. home.php <?php $pagetitle = 'Welcome to ENSI'; require 'template/header.php'; ?> <p> This web page page was created on <strong> <?php echo date('l F jS \a\t Y h:i:s A \,'); if (date('H') < 12) { echo ' RISE AND SHINE!!!,'; } else { echo ' TIME TO SLEEP!!!,'; } ?> </strong> on the computer that is running on PHP. </p> <div> <p><strong>Please enter your name and birthday:-</strong></p> <form method="post" action="update.php"> <fieldset> <legend>Details</legend> <label>First Name:</label> <input type="text" name="name" /> <br/> <label> Surname:</label> <input type="text" name="sname" /> <br/> <label> Address </label> <input type="text" name="address" /> <br/> <label>Phone</label> <input type="text" name="number" /> <br/> <input type="submit" name="submit" value="submit" /> <input type="reset" name="reset" value="reset" /> </fieldset> </form> <?php if(isset ($_POST['submit'])) { $name = $_POST['name']; $sname = $_POST['sname']; $address = $_POST['address']; $number = $_POST['number']; echo htmlentities ($text); echo 'Hello, your first name is ' .$name; echo ' Your surname is ' .$sname; echo ' Your address is - ' .$address; echo ', Your phone number is - ' .$number; } ?> </div> <?php require 'template/footer.php'; ?> When I fill in the form and then press submit it goes to "error updating database" from the script, so have I got the server information wrong? Thanks Will. Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/ Share on other sites More sharing options...
MrXHellboy Posted March 24, 2011 Share Posted March 24, 2011 No, It's your query mysql_query($query) or die ('error updating database'); Remove the ID and the NULL as you probably filled in @ the field ID not null & auto_increment Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/#findComment-1191652 Share on other sites More sharing options...
willhub Posted March 24, 2011 Author Share Posted March 24, 2011 Oh the database the ID field is empty. Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/#findComment-1191657 Share on other sites More sharing options...
MrXHellboy Posted March 24, 2011 Share Posted March 24, 2011 Remove the ID and the NULL as you probably filled in @ the field ID not null & auto_increment Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/#findComment-1191659 Share on other sites More sharing options...
willhub Posted March 24, 2011 Author Share Posted March 24, 2011 <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ $name = $_POST['name']; $sname = $_POST['sname']; $address = $_POST['address']; $number = $_POST['number']; mysql_connect("localhost", "", "") or die ('error: ' .mysql_error()); mysql_select_db ("bza410_test"); $query="INSERT INTO testTable (firstname, surname, address, phone)VALUES ('".$name."', '".$sname."', '".$address."', '".$number."')"; mysql_query($query) or die ('error updating database'); echo "database updated with: " .$name. " ".$sname." ".$address." ".$number ; ?> Still does not work and I have removed ID and NULL.... Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/#findComment-1191660 Share on other sites More sharing options...
MrXHellboy Posted March 24, 2011 Share Posted March 24, 2011 you see the row @ DB ? Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/#findComment-1191662 Share on other sites More sharing options...
willhub Posted March 24, 2011 Author Share Posted March 24, 2011 I'm confused, so what am I missing now? Quote Link to comment https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/#findComment-1191674 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.