0

I'm sure this has been asked 1000 times, but I have looked all over and can't seem to get this to work.

form:

<form action="sendinfo.php" method="post">               
    <h4>ID:</h4>
    <input type="text" name="CustomerID">
<h4>First name:</h4>
    <input type="text" name="FirstName">
    <h4>Last Name:</h4>
    <input type="text" name="LastName">
    <h4>Street:</h4>
    <input type="text" name="Street">
    <h4>City:</h4>
    <input type="text" name="City">
    <h4>Zip:</h4>
    <input type="text" name="Zip">
    <h4>State:</h4>
    <input type="text" name="State">
    <h4>Phone:</h4>
    <input type="text" name="Phone">
    <h4>Email:</h4>
    <input type="text" name="Email">
    <input type="submit">
</form>

sendinfo.php

    <?php
    include('connection.php');
    $dbh = con();

    $dbh->query = "INSERT INTO Customer (CustomerID, FirstName, LastName, Street, City, State, Zip, Phone, Email)
    VALUES  ('$_POST[CustomerID]', ('$_POST[FirstName]', ('$_POST[LastName]', ('$_POST[Street]', ('$_POST[City]', ('$_POST[State]', ('$_POST[Zip]', ('$_POST[Phone]', ('$_POST[Email]')";
    if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); } echo “Your information was added to the database.”; mysql_close($connect); 

    ?>

connection.php

<?php
define("DB_HOST", "localhost");
define("DB_NAME", "Impact_Technologies");
define("DB_USER", "root");
define("DB_PASS", "password");

function con(){
    try {
        $db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
        return $db_connection;
    } catch (PDOException $e) {
        echo "Sorry, there was a problem connecting to the database." . $e->getMessage();
    }
}
?>

When submit is clicked, no messages displayed and no information entered into the db

2 Answers 2

2

I see what's the problem, ('$_POST[CustomerID]', ('$_POST[FirstName]', ('$_POST[LastName]', ('$_POST[Street]', ('$_POST[City]', ('$_POST[State]', ('$_POST[Zip]', ('$_POST[Phone]', ('$_POST[Email]')

You're opening a parenthesis before every value, it should be like this:

('$_POST[CustomerID]', '$_POST[FirstName]', '$_POST[LastName]', '$_POST[Street]', '$_POST[City]', '$_POST[State]', '$_POST[Zip]', '$_POST[Phone]', '$_POST[Email]')
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, copy and paste haha. I found one other thing plus what you said and its working! thank you so much!
you're very welcome, please mark the answer as correct :) !!
1

First, Simplify your code by doing this

    $id = $_POST['custormerID'];
    $firstName = $_POST['FirstName'];
//and so on

Secondly, Remove all the opening parenthesis before every value

    $dbh->query = "INSERT INTO Customer (CustomerID, FirstName, 
LastName, Street, City, 
State, Zip, Phone, Email)
    VALUES  ('$id', '$FirstName', 
'$LastName', '$Street', '$City', 
'$State', '$Zip', '$Phone', '$Email')";
    if (!mysqli_query($user_info, $connect)) { 
die('Error: ' . mysqli_error()); 
} 
echo “Your information was added to the database.”; mysql_close($connect); 

Thirdly, mysql is depreciated, use mysqli or PDO instead

    if (!mysql_query($user_info, $connect)) { 
die('Error: ' . mysql_error()); } 
echo “Your information was added to the database.”; mysql_close($connect);

do this instead:

 if (!mysqli_query($user_info, $connect)) {
 die('Error: ' . mysqli_error()); 
} 
echo “Your information was added to the database.”; mysqli_close($connect);

Side Note: Is either you use mysqli or PDO, don't use the both. You can learn about PDO http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers and http://www.w3schools.com/php/php_mysql_intro.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.