life4learn
life4learn

Reputation: 1

All strings or characters change into numeric 1 in mysql columns

I try to create a simple registration system. I found a problem, however, all my data which inserted into database has changed into numeric. It happens when I inserted those data via php, not command prompt or phpmyadmin. For example, I type first name, lastname, etc in the form, the value in my table is numeric 1. I don't know where this number came from. I browsing and browsing, and I don't know what is the keyword. Please see my code below and give suggestion or clear explanation.

//TABLE STRUCTURE
user_id INT (10) NOT NULL AUTO_INCREMENT,
fname VARCHAR (100) NOT NULL,
lname VARCHAR (100) NOT NULL,
username VARCHAR (100) NOT NULL,
city VARCHAR (150) NOT NULL,
access timestamp (15) NOT NULL,
primary key (user_id);

//DB CONNECTION
$host = "localhost";
$user = "root";
$pass = "";
$dbas = "user";
$conn = mysql_connect($host, $user, $pass) or die(mysql_error("not connected"));
$data = mysql_select_db($dbas, $conn) or die(mysql_error("couldn't find"));

//FORM PROCCESS --> proccess.php
$id = isset($_POST['user_id']);
$fname = mysql_real_escape_string(isset($_POST['fname']));
$lname = mysql_real_escape_string(isset($_POST['lname']));
$username = mysql_real_escape_string(isset($_POST['username']));
$city = mysql_real_escape_string(isset($_POST['city']));
$access = date("m-d-y");

if(isset($_POST['submit']) !== '')
{
    if($id == '' && $fname == '' && $lname == '' && $username == '' && $city == '' && $access == ''){
        echo "please fill empty";
    } else {
        $sql = mysql_query("INSERT INTO user (user_id, fname, lname, username, city, access) VALUES (null, '".$fname."', '".$lname."', '".$username."', '".$city."', '".$access."')");

        if($sql)
        {
            echo "success";
        }
    }
}

//RETRIEVE DATA
$query = "SELECT * FROM user";
$result = mysql_query($query);
while($getit = mysql_fetch_array($result))
{
    echo "<table>";
    echo "<tr>";
    echo "<td>" .$getit['fname']. "<td/>";
    echo "</tr>";
    echo "</table>";
}

// FORM STRUCTURE
<form action="proccess.php" method="post">
    <input type="hidden" name="user_id">
    <input type="hidden" name="access">
    <input type="text" name="fname">
    <input type="text" name="lname">
    <input type="text" name="username">
    <input type="text" name="city">
    <button type="submit" name="submit">REGISTER</button>
</form>

Thanks in advance

Upvotes: 0

Views: 43

Answers (2)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You have the following in your code:

$fname = mysql_real_escape_string(isset($_POST['fname']));

isset() returns a boolean true or false, not the actual value itself. So essentially, you'll just be doing:

$fname = mysql_real_escape_string(1);

From the documentation for isset():

Return Values

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Upvotes: 5

GrahamTheDev
GrahamTheDev

Reputation: 24825

$city = mysql_real_escape_string(isset($_POST['city']));

Breaking it down -

Escape the string to make it safe(r)

Do we have the post for city

This returns 1 for yes and 0 for no.

but you are not actually getting the value.

$fname = mysql_real_escape_string(isset($_POST['fname']) == 1 ? $_POST['fname'] : ""));

This will check if it is set - if it is set $city to the post value, otherwise enter a blank value (? works like an if statement : is like an else)

Accept Abhik Chakraborty answer - dont want to snipe him just wanted to show you how to fix it!

Upvotes: 0

Related Questions