I have a form that I am using to post data to mysql.
Before submitting the form I want to check the database and see if there are any fields in the column ‘customerid’ that equal ‘userid’ and if so not to post the form.
Basically, I am trying to limit my users from posting more than once. Users will be able to login to my system and make ONE post. They will be able to delete and modify their post but are only limited to one post.
How would I do this??? Code so far:
<?php
include '../login/dbc.php';
page_protect();
$userid = $_SESSION['user_id'];
$sql="INSERT INTO content (customerid, weburl, title, description)
VALUES
('$_POST[userid]','$_POST[webaddress]','$_POST[pagetitle]','$_POST[pagedescription]')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
?>
You should use two queries. The first,
SELECT customerid FROM content WHERE customerid = @IDwill return a row if there already exists a record with that ID. From there, an if statement will either tell your user that they already have a post or proceed with your existing code to insert the record.