Jump to content

data not being added


phppatron

Recommended Posts

My file is a learning exercise.  It has a simple form, and it uses the inserted data and is supposed to add the data to my simple database.  It seems to be collecting the data (at least, if I echo the gathered data it shows properly) - so I don't know why the data is not uploaded.  I understand it could be several things - but I don't know where to begin checking.  I'm not getting any errors.

 

Here is my file:  (I removed the actual user and password and the database name substituted placeholders here)     

 

more info:  the table has the fields in the form plus one more -  ID (set to autoincrement)      If nothing is wrong here, where should I begin to look??

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

  <title></title>

</head>

 

<body>

 

<?php

$street = $_POST["street"];

$city = $_POST["city"];

$stateabbr = $_POST["stateabbr"];

$zipcode = $_POST["zipcode"]; ?>

 

<?php

 

 

$connection = mysql_connect("localhost", "user", "password");

if (!$connection){

die("Could not connect to the database: <br/>" . mysql_error());

}

 

 

$db_select = mysql_select_db("databasename");

if (!$db_select) {

 

die ("could not select the database: <br />". mysql_error());

}

?>

 

<?php  if (!empty($street)&& !empty($city) && !empty($stateabbr) && !empty($zipcode)){

      $query="INSERT INTO 'table' VALUES

('$_POST[street]','$_POST[city]','$_POST[stateabbr]',' $_POST[zipcode])";

$result = mysql_query($query);

}

echo $query;

?>

 

    <form action="<?php echo($_SERVER['PHP_SELF']); ?>"

        method="post">

        <label>

            Street: <input type="text" name="street" value="<?php echo $street; ?>"/>

        </label>

        <label>

            City: <input type="text" name="city" value="<?php echo $city; ?>" />

        </label>

        <label>

            State Abbrev: <input type="text" name="stateabbr" value="<?php echo $stateabbr; ?> "/>

        </label>

        <label>

            Zip Code: <input type="text" name="zipcode" value="<?php echo $zipcode; ?>" />

        </label>

        <input type="submit" value="Submit" />

    </form>

</body>

 

</html>

 

 

Link to comment
Share on other sites

One good debugging trick is to go right below the query, copy whatever is in the query, and then die with it.

 

So, right where you have:

 

$result = mysql_query($query);

 

add below it:

 

echo mysql_error();

die($query);

 

Three things are learned from this.

 

1) You know the query is getting called.

2) You know the exact values going into it.

3) You know if there are any mysql errors.

 

If that still doesn't work, copy the query and try to put it in PHP myadmin, and see if it acctually does something in mysql.

Link to comment
Share on other sites

I think that you need the field names in the query:

$query="INSERT INTO 'table' VALUES

('$_POST[street]','$_POST[city]','$_POST[stateabbr]',' $_POST[zipcode])";

 

should be:

$query="INSERT INTO 'table' (street, city, stateabbr, zipcode ) VALUES

('$_POST[street]','$_POST[city]','$_POST[stateabbr]',' $_POST[zipcode])";

 

Link to comment
Share on other sites

Correct to these:

$street = $_POST['street'];
$city = $_POST['city'];
$stateabbr = $_POST['stateabbr'];
$zipcode = $_POST['zipcode']; 

 

$query="INSERT INTO table VALUES
($_POST['street'],$_POST['city'],$_POST['stateabbr'], $_POST['zipcode'])";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.