Asked  7 Months ago    Answers:  5   Viewed   33 times

I've looked for similar questions with no success.

I have this piece of code:


$query  = "INSERT INTO table1 ";
$query .= "(fname, lname, mail)";
$query .= " VALUES ";
$query .= "('".$_POST[fname]."', '".$_POST[lname]."', '".$_POST[mail]."')"; 

$result = mysql_query($query) or die ("Query Failed: " . mysql_error());

And I want that the script will check if the value inserted exists in the corresponding column, and throw an error if it does. any ideas?



Create a UNIQUE key on the fields you care about, and detect the integrity error after the fact.

Wednesday, March 31, 2021
answered 7 Months ago

It's better to set a constraint on your columns to prevent duplicate data instead of checking and inserting.

Just set a UNIQUE constraint on imdbid:

ALTER TABLE `requests` ADD UNIQUE `imdbid_unique`(`imdbid`);

The reason for doing this is so that you don't run into a race condition.

There's a small window between finishing the check, and actually inserting the data, and in that small window, data could be inserted that will conflict with the to-be-inserted data.

Solution? Use constraints and check $DBH->error() for insertion errors. If there are any errors, you know that there's a duplicate and you can notify your user then.

I noticed that you are using this, $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);. In this case, you don't need to check ->error() because PDO will throw an exception. Just wrap your execute with try and catch like this:

$duplicate = false;

try {
} catch (Exception $e) {
    echo "<p>Failed to Request ".$_POST['imdbid']."!</p>";
    $duplicate = true;

if (!$duplicate)
    echo "<p>Successfully Requested ".$_POST['imdbid']."! Thanks!</p>";
Saturday, May 29, 2021
answered 5 Months ago

Your checkData.php script is returning a Boolean value (true or false) but it is not returning a complete HTTP response. Your script should return a complete HTTP response, and encode the results in something like JSON format, like this:

$data = [ 'exists' =>
  (fopen($cat1,"r") || fopen($cat2,"r"))
header('Content-Type: application/json');
echo json_encode($data);

Then, your JS .done() function should parse the returned JSON response, examine the value returned by the server, and respond accordingly, like this:

  if (data.exists) { // 'exists' is the key returned by PHP
    console.log('The file exists!');
  } else {
    console.log('The file does NOT exist!');
Saturday, May 29, 2021
answered 5 Months ago

you can't print the result from mysqli_query, it is mysqli_resource and for dumping the error you need to change mysql_error() to mysqli_error()

$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$result = mysqli_query($db, $sql1) or die(mysqli_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    echo $row['id'].'<br>'; 
Saturday, May 29, 2021
answered 5 Months ago

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/

Tuesday, July 13, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :