How do i allow duplicate entries in my php file from this file ... i have the html file which is a form to track radios for a company. I have text boxes and radio buttons and all the fields need to be able to have duplicate entries except for the serial number field. i keep getting and error saying i cannot have duplicate entries.
<?php
$con = mysql_connect("localhost","Jason","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql="INSERT INTO test (
firstname,
lastname,
department,
radiomodel,
serialnumber,
issuedate
)
VALUES(
'$_POST[firstname]',
'$_POST[lastname]',
'$_POST[department]',
'$_POST[radiomodel]',
'$_POST[serialnumber]',
'$_POST[issuedate]'
)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
header( "refresh:150;url=testinput.php" );
?>
Your serialnumber is a primary key and thus you can not have duplicate values.
You should remove the primary key from the serialnumber field and add in a new id column to put the primary key on this or use multiple columns together as the key, for example use firstname, lastname and date and serialnumber as the key.
Really you should split this off into multiple tables though.
Related
I have read many of the postings regarding current time stamp and tried many of the solutions provided, however none of them fixed my issue. I need a current time stamp to be added to the database. I think my code may be the reason why the other solutions are not working. Everything else post perfectly. The time stamp just gives me all "0", the other solutions I tried gave me a "line 6 error".
Here is my current code
<?php
$con = mysql_connect("mysql","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
/* Prevent duplicate submissions */
if (isset($_COOKIE['FormSubmitted']))
{
show_error('You may only submit this form once per session!');
}
mysql_select_db("seller", $con);
$sql="INSERT INTO listing (name, email, website, url, dropdown, price, date, visitors, income, host, description)
VALUES
('$_POST[name]', '$_POST[email]', '$_POST[website]', '$_POST[url]', '$_POST [dropdown]', '$_POST[price]', '$_POST[date]','$_POST[visitors]', '$_POST[income]', '$_POST[host]', '$_POST[description]', 'CURRENT_TIMESTAMP[subdate]' )";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Thank you for listing with us. Explore Now";
mysql_close($con)
?>
Using CURRENT_TIMESTAMP in the PHP context has no sense!
You have to include the needed default value in your table definition.
Something like this:
CREATE TABLE listing
...
description VARCHAR(...)
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
...
Then in PHP you should only write:
$sql="
INSERT INTO listing (name, ..., description)
VALUES ('$_POST[name]', ..., '$_POST[description]')
";
In my PHP code, I save a record like this:-
mysqli_query($con, "INSERT INTO levels (levelName)
VALUES ('" . $_POST["levelName"][0] . "')");
And this works fine.
In the table 'levels', there is an auto-incrementing PK field called "ID". How would I go about returning/echoing the value of that field when the record is saved to the database?
Quoting from the following reference:
How to Get the Unique ID for the Last Inserted Row
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
# http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
-
Example
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db",$con);
$sql = "INSERT INTO person VALUES ('Børge','Refsnes','Sandnes','17')";
$result = mysql_query($sql,$con);
echo "ID of last inserted record is: " . mysql_insert_id();
mysql_close($con);
?>
from: http://www.w3schools.com/php/func_mysql_insert_id.asp
use
mysql_insert_id()
function
if using mysqli use something like this
$mysqli->insert_id
I have mysql data base in which i am adding data in mysql data base but problem is that it only stores only one record not more than that.
my table structure is
<?php
$con = mysql_connect("example.com","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("surveyipad", $con);
$response_id=$_POST['response_id'];
$participant_id=$_POST['participant_id'];
$question_id=$_POST['question_id'];
$answer_text=$_POST['answer_text'];
$answer_option=$_POST['answer_option'];
$query=("INSERT INTO survey_question_responses (response_id,participant_id,question_id,answer_text,answer_option)
VALUES ('', '$participant_id','$question_id','$answer_text','$answer_option')");
mysql_query($query,$con);
printf("Records inserted: %d\n", mysql_affected_rows());
echo($response_id)
?>
response id is primary key in table and also set to auto increment
Try like this
$query=("INSERT INTO survey_question_responses (participant_id,question_id,answer_text,answer_option)
VALUES ('$participant_id','$question_id','$answer_text','$answer_option')");
As you made the id field auto incremented don't insert it vie your INSERT query .
Write the query as
INSERT INTO survey_question_responses (participant_id, question_id, answer_text, answer_option)
VALUES ('$participant_id', '$question_id', '$answer_text', '$answer_option')
You should also explain or send the structure of your table.
what should do if the entry are doubled?
<?php
require_once('auth.php');
session_start();
$exam = $_SESSION['exam'];
$subject_id = $_SESSION['exam'];
$_SESSION['sub'] = $subject_id;
$subject_title = $_POST['subject_title'];
$subject_description = $_POST['subject_description'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_compre', $con);
$sql = "INSERT INTO examsubjectrecord_table(subject_id , subject_title ,
subject_description)
VALUES ('$subject_id','$subject_title', '$subject_description')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
header("location: addsubject.php?exam=".$exam ."");
}
?>`
Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\compre\admin\addsubjectacc.php on line 4
**Error: Duplicate entry '1' for key 'PRIMARY'**
It depends on yout application business-logic.
You can notify a user about a duplicated entry or silently update information with INSERT ... ON DUPLICATE KEY UPDATE ... SQL statement.
In your database you have a primary key of subject_id which cant have duplicates.
If you need to have duplicates in the subject_id column then you should add a column and set it as a primary key in your database. For example add another column unique_id and set it to auto_increment and as a primary key for row identification.
Basically, you'll first want to check if the value you're trying to insert into your primary key field already exists.
So if you primary key field is subject_id, you'd need to check if that already exists by doing a select query followed by PHP's mysql_num_rows function. For example:
$subject_id = 1337;
$check = mysql_query("SELECT `subject_id` FROM `examsubjectrecord_table` WHERE `subject_id`=" . $subject_id);
// See if anything was returned
if(mysql_num_rows($check) > 0) {
// We have something with this subject_id already!
echo "Cannot insert duplicate subject!";
} else {
// All clear, run your INSERT query here
}
Which column is your primary key? I'm going to assume that's subject_id. This needs to be unique for each row in your table. The easiest way to ensure this is to use AUTO_INCREMENT and then avoid inserting the subject_id at all. It will be assigned automatically.
If you need to find out what the ID of new subjects is, you can use mysql_insert_id.
I was hoping to get a little insight on this.
What I have is a form collecting firstname, lastname, city, state, and email address. This form is using jquery validation plugin and the form plugin.
I would like to check if email already exists... if so then spit out a message that tells them they already exist.
This is what I have for my update.php script in which the form if using to add names to the mysql database:
<?php
$con = mysql_connect("host","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("table", $con);
$sql="INSERT INTO wallnames (ID, firstname, lastname, city, state, email)
VALUES('NULL','$_POST[firstname]','$_POST[lastname]','$_POST[city]','$_POST[state]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<p style=\width:350px; height:200px; vertical-align:middle;\><strong>Thank you for adding your info</strong></p>";
mysql_close($con);
?>
You should create a unique index on the email column, then the insert will fail if you try to insert the same email twice.
CREATE UNIQUE INDEX ux_wallnames_email ON wallnames (email)
You can also run this query to test if an email already exists:
SELECT EXISTS (SELECT NULL FROM wallnames WHERE email = 'test#example.com')
It will return either 0 if the email is unused, or 1 if it already exists in the table.