Jump to content

Blank rows submitted to database


VTXmom
Go to solution Solved by IamCrazyD,

Recommended Posts

I am trying to complete a class assignment of building a website, pretty much a final project, and I am having an issue with one of my pages. I created a form that a user fills out and submits to a mysql database. My issue is that it is adding a row to the database, but it is unpopulated. I will be the first to admit that I'm not sure what I am doing, and was wondering if someone could look at my code and tell me where I am going wrong. I've googled this issue and tried all the fixes that were posted but nogo. Thanks in advance for any help. Here is the code for my php script:

 

<?php
$link = mysqli_connect('localhost', '****', '******', 'pet_shop');

if($link === false) {
   die("ERROR: Could not connect. " . mysqli_connect_error());
}

  error_reporting(E_ALL ^ E_NOTICE);
 
$firstname = isset($link, $_POST['FirstName'])? $_POST['$firstame'] : '';
$lastname = isset($link, $_POST['LastName'])? $_POST['$LastName'] : '';
$address = isset($link, $_POST['Address'])? $_POST['$Address'] : '';
$city = isset($link, $_POST['City'])? $_POST['$City'] : '';
$state = isset($link, $_POST['State'])? $_POST['$State'] : '';
$zip = isset($link, $_POST['Zip'])? $_POST['$Zip'] : '';
$phonenumber = isset($link, $_POST['PhoneNumber'])? $_POST['$PhoneNumber'] : '';
$email = isset($link, $_POST['Email'])? $_POST['$Email'] : '';
$pettype = isset($link, $_POST['PetType'])? $_POST['$PetType'] : '';
$breed = isset($link, $_POST['Breed'])? $_POST['$Breed'] : '';
$petname = isset($link, $_POST['PetName'])? $_POST['$PetName'] : '';
$neuteredorspayed = isset($link, $_POST['NeuteredOrSpayed'])? $_POST['$NeuteredOrSpayed'] : '';
$petbirthday = isset($link, $_POST['PetBirthday'])? $_POST['$PetBirthday'] : '';

$sql = "INSERT INTO `pet_shop`.`grooming` (`FirstName`, `LastName`, `Address`,
`City`, `State`, `Zip`, `PhoneNumber`, `Email`, `PetType`, `Breed`, `PetName`,
`NeuteredOrSpayed`, `PetBirthday`) VALUES ('$firstname', '$lastname', '$address',
'$city', '$state', '$zip', '$phonenumber', '$email', '$pettype', '$breed',
'$petname', '$neuteredorspayed', '$petbirthday');";

if(mysqli_query($link, $sql)) {
   echo "A member of our staff will contact you to set up an appointment..";
}
else
{
   echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}

mysqli_close($link);
?>
 

Link to comment
Share on other sites

I can't see what's giving you trouble. It's difficult to read the code when it's not formatted. (I'm a noob too!) You should use the full editor when posting and select the "code-option" for better readability! But anyway, here is a basic set up for retrieving and sending values from an HTML form to your database. Maybe it will help.

<form method="POST" action="$_SERVER['PHP_SELF']">

     Name: <input type="text" name="my_name">
     Phone: <input type="number" name="my_phone">

     <input type="submit">

</form> 
<?php

$dbhost = "localhost";
$dbuser = "my_username";
$dbpass = "my_password";
$dbname = "my_database";

$dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

$value1 = "$_POST['my_name']";
$value2 = "$_POST['my_phone']";

$query = "INSERT INTO table_name (column1, column2) VALUES ('$value1', '$value2');
mysqli_query($dblink, $query);

?>

Make sure your database is defined correctly, and see if you can get this basic set up to work. Then, expand one line of code at a time and you should be able to get where you want to be! :)

Edited by marius_haugan
Link to comment
Share on other sites

  • Solution

Your code:

$firstname = isset($link, $_POST['FirstName'])? $_POST['$firstame'] : '';

My Code:

$firstname = isset($_POST['FirstName']) ? mysqli_real_escape_string( $link, $_POST["FirstName"] ) : '';

From the ducumentation: For more information, visit here: http://php.net/manual/en/function.isset.php

isset — Determine if a variable is set and is not NULL
bool isset ( mixed $var [, mixed $... ] )

In $_POST after ? in your code, you have passed the variable which will return ''; And hence, you are not seeing that the values are inserted in the database.

 

So your code should look like this:

<?php

error_reporting(E_ALL ^ E_NOTICE);

$link = mysqli_connect( 'localhost', '****', '******', 'pet_shop');

if( !$link ) {
   die( "ERROR: Could not connect: " . mysqli_connect_error( $link ) );
}

//You need to use mysqli_real_escape_string() method to avoid sql injection;
// Refer this link for more information http://php.net/manual/en/mysqli.real-escape-string.php

$firstname = isset( $_POST['FirstName'] ) ? mysqli_real_escape_string( $link, $_POST['FirstName'] ) : '';
$lastname  = isset( $_POST['LastName'] ) ? mysqli_real_escape_string( $link, $_POST['LastName'] ) : '';
$address   = isset( $_POST['Address'] ) ? mysqli_real_escape_string( $link, $_POST['Address'] ) : '';
//and so on...

$sql = "INSERT INTO `pet_shop`.`grooming` (`FirstName`, `LastName`, `Address`, ... ) VALUES ( ' " . $firstname . " ', ' " . $lastname . " ',  ' " . $address . " ' , ...)";

$result = $mysqli_query( $link, $sql );

if( $result ) {
    // remove the echo line or else you will face a warning of cannot modify header.
    // You can create a new file and write the echo statement in that file
    // You can then call that file in the header function below.
    echo "A member of our staff will contact you to set up an appointment..";

    // To avoid twice insertion in the database on page refresh
    header("Location: /path/to/your/file.php");

    // To stop the execution of the current page.
    exit();
} else {
   echo "ERROR: Could not execute the query. " . mysqli_error( $link );
}

mysqli_close( $link );
?>
Edited by IamCrazyD
Link to comment
Share on other sites

I can't see what's giving you trouble. It's difficult to read the code when it's not formatted. (I'm a noob too!) You should use the full editor when posting and select the "code-option" for better readability! But anyway, here is a basic set up for retrieving and sending values from an HTML form to your database. Maybe it will help.

 

<form method="POST" action="$_SERVER['PHP_SELF']">

     Name: <input type="text" name="my_name">
     Phone: <input type="number" name="my_phone">

     <input type="submit">

</form> 
<?php


Marius, thanks,  where do I find the full editor?

$dbhost = "localhost";
$dbuser = "my_username";
$dbpass = "my_password";
$dbname = "my_database";

$dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

$value1 = "$_POST['my_name']";
$value2 = "$_POST['my_phone']";

$query = "INSERT INTO table_name (column1, column2) VALUES ('$value1', '$value2');
mysqli_query($dblink, $query);

?>
Make sure your database is defined correctly, and see if you can get this basic set up to work. Then, expand one line of code at a time and you should be able to get where you want to be! :)
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.