Insert no duplicate name PHP - php

I have this form and this function for insert name in db:
<form action="player.php#insertPlayer" method="post">
<input type="text" name="nameplayer" />
<input type="submit" value="Ajouter joueur" />
</form>
function insertPlayer($name) {
db connection
if (empty ($name))
{echo "<span style='color: red'>WRONG!!!!</span>";}
else
{
$insertplayer="INSERT INTO `player`(id, name) VALUES ('','$name');";
echo 'player insert succes';
mysql_close($db);
}
}
But if i enter 2 same name, it's work, how can i do for just have always one same name ?

To make player table column name as UNIQUE, run the below ALTER statement on your corresponding Database.
ALTER TABLE player ADD UNIQUE (name);
If already the player table has some data with duplicate values in name column, the alter statement will fail. So clear the data from player table before running the ALTER statement.

Related

How to insert row id to another table field using php mysql?

I have first table name is conference.
I want to insert table conference row id (81) to another second table name conf_dates
and that have field conf_id use for save conference row id
The issue is that value is empty insert in second table conf_dates as shown in second table image
Here is php code
<?php
$id = $_GET["id"];
$trans = array("strtdate"=>$strtdate,"enddate"=>$enddate,"conf_id"=>$id);
$query = $db->insert($trans,PREFIX."conf_dates");
?>
Here is html code
<p>
<label>Conference ID</label>
<input name="conf_id" class="text-input medium-input" type="text" id="conf_id" value="" />
</p>
Please tell me how to insert row id to another table field?
what are the values of $strtdate, $enddate
Also please share the table structure of "conf_dates" .
$id = intval($_GET["id"]);
check you are getting correct types, and values for variables you trying to insert
You named the input field conf_id while you try to insert $_GET['id'].
You should insert $_GET['conf_id']` instead.

How to select tables from MySQL Database in php?

I am trying to create a form in PHP with MySQL involved.
The theme of this form, form.php, is all about drinks. There are thirteen tables -each table named after type of drink (Belgian beer, wine, soda, rum, ...) that all include only three columns: Id of the drink(PRIVATE KEY AUTO_INCREMENT), the name of the drink and the price (in char). The rest of the columns in other tables are the same, the values are different.
I want to create a dropdown that allows you to select tables from MySQL to add/delete data to the columns within the selected table.
Some names of tables, columns and form elements are named in Dutch.
Click here to see resources
These are the things I do know:
1)Php code to add data to database
<?php
if ( ! empty($_POST))
{
$mysqli = new mysqli('localhost','root','','welkom');
if ($mysqli ->connect_error)
{
die('connect error: '. $mysqli->connect_errno . ': ' . $mysqli->connect_error);
}
$sql = "INSERT INTO abdijbieren (naam, prijs) VALUES ('{$mysqli->real_escape_string($_POST['naam'])}','{$mysqli->real_escape_string($_POST['prijs'])}')";
$insert = $mysqli->query($sql);
if ($insert)
{
echo "Success! Row ID: {$mysqli->insert_id}";
}
else
{
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}?>
2)Form
<form method="post" action="check.php"> <!--check.php is not made yet but when that happens it should be used to automatically update the selected table and auto-return to form.php -->
<input name="naam" type="text" placeholder="naam drank/snack" required><br>
<input name="prijs" type="text" placeholder="prijs" required><br>
<input type="submit" value="Submit form">
</form>
All I can do with it is to add data to one certain column.
If you know how I can solve this, you are my hero.
You can select table names like this:
SHOW TABLES FROM <your_database>
Also to select columns from a table:
SHOW COLUMNS FROM <your_table>

prevent insert data to database if specific atribute already exist

I want to prevent duplicate values into a database table from a form using PHP.
I have a table data on database that have atribute dataid(auto increment as primary key), data1, data2.
And I have a simple form like this
<h2>Enter your data</h2>
<form action="script.php" method="post">
Data 1:<input type="text" name="data1" /></p>
Data 2:<textarea name="data2"></textarea></p>
<input type="submit" name="submit" value="Add Data" />
</form>
It's script.php for inserting data to database
<?php
if(isset($_POST['submit']))
{
//connect to the database
$conn = mysql_connect('host', 'username', 'password', 'dbname') or die(mysql_error());
//insert results from the form input
$query = "INSERT INTO data(data1, data2) VALUES('$_POST[data1]', '$_POST[data2]')";
$result = mysql_query($conn, $query) or die(mysql_error());
}
?>
but it will insert any data from the form to database.
How can I prevent insert data to database if data1 already exist?
The solution to your problem is to make the column unique so the database takes care of enforcing this constraint. You can do this by creating a unique index or constraint:
alter table data add constraint unq_data_data1 unique (data1);
You can then use this in an insert to ignore duplicates by using on duplicate key update:
INSERT INTO data(data1, data2)
VALUES('$_POST[data1]', '$_POST[data2]')
ON DUPLICATE KEY UPDATE data1 = VALUES(data1);
The ON DUPLICATE KEY part doesn't really do anything. It is a no-op.
Also, you should parameterize your queries, so you are not subject to SQL injection and so the query plans can be cached. But that is another matter.
make column data1 unique
ALTER TABLE `data` ADD UNIQUE (`data1`);
change your query like this to ignore insert when data exist
$query = "INSERT IGNORE INTO data(data1, data2) VALUES('$_POST[data1]', '$_POST[data2]')";

ON DUPLICATE KEY UPDATE value; inserting same values twice

I am trying to insert some values and if there is a DUPLICATE KEY UPDATE table person. I am getting the insert of values but it is repeating the values instead of doing an update. The input fields are dynamic and values can be added or deleted. But I am trying to not have repeating values. How can insert new values and if there is a duplicate key to then update the fields?
Html/(jquery function not included generates more input fields)
<ul id="pq_entry_1" class="clonedSection">
<li>
<input id="person_fname_1" name="person_fname_1" placeholder="Person #1 - First Name" type="text" />
</li>
<li>
<input id="person_lname_1" name="person_lname_1" placeholder="Last Name" type="text" />
</li>
</ul>
<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='delete Delete' />
PHP/Mysql Query
//Insert or Update Values
$f = 1;
while(isset($_POST['person_fname_' . $f]))
{
$person_fname = $_POST['person_fname_' . $f];
$person_lname = $_POST['person_lname_' . $f];
$query_init3 = "INSERT INTO person (academy_id, first_name, last_name) VALUES (:id,:person_fname,:person_lname)
ON DUPLICATE KEY UPDATE academy_id=:id, first_name=:person_fname, last_name=:person_lname";
$query_prep3 = $db_con->prepare($query_init3);
$query_prep3->execute(array(
"id" => $id,
"person_fname" => $person_fname,
"person_lname" => $person_lname
));
$f++;
}
Table after query:
All sort of duplication check SQL
REPLACE INTO
INSERT .. ON DUPLICATE
INSERT IGNORE
works when table has Primary key or UNIQUE index. I think there is no UNIQUE INDEX. (maybe person_id is PK for AUTO_INCREMENT)
so, could you post your person's create statement? If person has not UNIQUE(person_fname, person_lname) you should add it with following sql:
ALTER TABLE person ADD UNIQUE (person_fname, person_lname);

Update row if there is a value or insert if not

I have a form where the user inserts data but they can go back to the same page to edit their information. My table structure is:
id (auto int index),
user id (links to other tables),
Doc_Name,
Abstract
I have an insert query:
$user_id = intval($_SESSION['user_id']);
$Doc_Name = mysql_real_escape_string($_POST['Doc_Name']);
$abstract = mysql_real_escape_string($_POST['abstract']);
$the_query = sprintf("INSERT INTO `document` (`user_id`,`Doc_Name`,`abstract`) VALUES
('%d','%s','%s')", $user_id, $Doc_Name, $abstract);
However, if their is already a row for this user_id then I want the update query instead:
mysql_query("UPDATE document SET `Doc_Name` = '$Doc_Name', 'abstract='$abstract'
WHERE id='$_SESSION[user_id]'") or die(mysql_error());
Also, so the user knows what they entered, I tried to use this echo in the text box but that didn't work either,
<textarea name="Doc_Name" style="width:500px; height:150px" type="text" id="Doc_Name"
value="<? echo $row_settings['Doc_Name']; ?>" size="300"> </textarea>
You can use INSERT ... ON DUPLICATE KEY UPDATE Syntax
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE
c=c+1;
You want the INSERT ... ON DUPLICATE UPDATE syntax
for the textarea you can use
<textarea name="Doc_Name" style="width:500px; height:150px" type="text" id="Doc_Name" size="300"><? echo $row_settings['Doc_Name']; ?></textarea>
everything between the tags is displayed and editable
EDIT: to the other posters: nice, did not know INSERT ON DUPLICATE
the query:
SELECT * FROM document WHERE id='{$SESSION['user_id']}'
php:
if(mysql_num_rows(mysql_query($query)) > 0) {
//code to be executed if id exists
}

Categories