/    Sign up×
Community /Pin to ProfileBookmark

nothing being sent to the database except the ID

For some reason when i submit a form data to the database, that that seems to get stored is the random ID

Ive attached the PHP code below that im using… can you see any issues with the code that might be stoping both email addreses, name and message not being store in the database

[code=php]// connect to database
$link = mysql_connect ($host, $user, $password);
mysql_select_db ($dbname, $link);

// creates a random number for the id, ans check to see if the random number currently exists in database
$success = FALSE;

while($success == FALSE) {
$rand = rand(100000, 999999);

$q = “SELECT * FROM $tablename WHERE rand = ‘$rand'”;
$r = mysql_query($q, $link);

echo mysql_error();

if(mysql_num_rows($r)) { //id exists
continue;
} else {
$success = TRUE;
}
}

// insert your data here with $rand as the id
$firstname = mysql_real_escape_string($_POST[‘youremail’]);
$lastname = mysql_real_escape_string($_POST[‘name’]);
$email = mysql_real_escape_string($_POST[‘receiveremail’]);
$datepicker = mysql_real_escape_string($_POST[‘message’]);

$query=”INSERT INTO $tablename (rand, youremail, name, receiveremail, message)
VALUES (‘”.$rand.”‘, ‘”.$youremail.”‘, ‘”.$name.”‘, ‘”.$receiveremail.”‘, ‘”.$message.”‘)”;

mysql_query($query) or die (mysql_error()); [/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@simplypixieFeb 05.2013 — Look at your code closely, there is a glaring error:
[code=php]$firstname = mysql_real_escape_string($_POST['youremail']);
$lastname = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['receiveremail']);
$datepicker = mysql_real_escape_string($_POST['message']);

$query="INSERT INTO $tablename (rand, youremail, name, receiveremail, message)

VALUES ('".$rand."', '".$youremail."', '".$name."', '".$receiveremail."', '".$message."')"; [/code]


In your query you are trying to insert the values for variables $youremail, $name, $receiveremail, $message - yet nowhere in your script do you have these variables declared and assigned values. What you do have are the variables $firstname, $lastname, $email, $datepicker (which don't make any sense in themselves as they don't relate to the posted data) and therefor it should be those variables that you use in your query.

The $rand number is being inserted as you declare it and assign a value early on in your script.
Copy linkTweet thisAlerts:
@popcopauthorFeb 05.2013 — oh dear, it was late last night, must be why i missed this
×

Success!

Help @popcop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...