I Like
I Like

Reputation: 1847

INSERT INTO not working for Mysql database

I'm a novice at mysql having trouble with inserting fields. I have created a login form and a homepage form for users to enter personal details. The login form submitted to mysql database fine, but the personal details form refused to submit; only when I specified the same fields as the first form did the form submit. Obviously, I would like to add new data in the second form, and I would appreciate tips on organizing my database, which consists of a single table profile.It stores info on every user including fields: id,username,password,avatar,location,goal, etc.

Appreciate your help & patience. I will combine the info from the two forms into a user record eventually. Right now, though, I would like to know why no new entry is created or error message displayed even though my error display is turned on.

EDIT:: sorry for not including whole code

loginform.php (works fine)

<?php
require("connect.php");
session_start();
$query = "SELECT * FROM `profile`";
if ($query_run=mysqli_query($cnn,$query))
{
    echo "
    <h1>Sign up for Runners World</h1>
    <form action='' method='post'>
    Username: <input type='text' name='username'><br> 
    Email: <input type='text' name='email'><br>
    Password: <input type='text' name='pw'><br>
    <input type='submit' name='submit' value='submit'>
    </form>
    ";
}
else {
die("<br>could not connect to table");
}
?>
<?php
if (isset($_POST['submit']))//if you submitted the form
{

$username = $_POST['username'];
$password = $_POST['pw'];
$email = $_POST['email'];

    if ($username and $password and $email)
    {
    $addLogin = "INSERT INTO profile (username,email,password) VALUES ('$username','$email','$password')";
    $success = $cnn->query($addLogin);
        if ($success)
        {
            $_SESSION['name']="$username";
            echo("<script>location.href = 'homePage.php';</script>");
        }
        else 
        {
            die("login data failed to reach database");
        }
    }
    else {echo "please fill out all the fields";}   
}
else {
$submit=null;
echo 'no form submitted';
}


?>

addDetails.php (not submitting)

<?php
session_start();
error_reporting(E_ALL);
ini_set("display_errors",1);
require("connect.php");
require("login.php");
echo "<h1>Welcome ".$_SESSION['name']."</h1>";

echo "<form action='' method='post'>
Avatar: <input type='text' name='avatar'><br>
Location: <input type='text' name='location'><br>
Descripiton: <input type='text' name='about'><br>
Daily Goal: <input type='text' name='goal'><br>
<input type='submit' value='submit' name='submit'>
</form>
";

$avatar = $_POST['avatar'];
$location = $_POST['location'];
$goal = $_POST['goal'];
$about = $_POST['about'];

if (isset($_POST['submit']))
{
$sql = "INSERT INTO profile (avatar,location,goal) VALUES ('$avatar','$location','$goal')";
    if ($cnn->query($sql)===TRUE)
        {echo "you have inserted profile fields";}
    else 
        {echo "sqlQuery failed to insert fields";}
}
?>

Upvotes: 0

Views: 107

Answers (2)

hanseve
hanseve

Reputation: 11

You should change the Sql statement in 'addDetails.php' to:

UPDATE profile 
SET avatar={$avatar}, location={$location}, goal={$goal} 
WHERE id={$id}

By the way you should never ever use this statement in your production, it is not safe, you should keep in mind to prevent Sql injection.

Upvotes: 1

Mark B
Mark B

Reputation: 682

If you want to add data to a row that already exists, look up the UPDATE command in SQL

Upvotes: 1

Related Questions