Jump to content

Problems connecting to database.


willhub

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/231579-problems-connecting-to-database/
Share on other sites

<?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....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.