tamas ladi
tamas ladi

Reputation: 1

PHP SQL - Update data of search result

I'm trying to make a site of promotion when the registered user can get a free beer. If the employee search for a someone his name, id and attendance show up. Attendance shows if they had gotten the beer already. If the employee press a button it marks in database that he has gotten the beer. My tries just dont work out, but I tried everything from what I found in forums. Please help me what's wrong in my code, it's driving me crazy.

So I have the results shown up working. I add a submit button with command to change the attendance's value from 0 to 1 WHERE id='%$result['id']%'

looks like this:

HTML

<input type="submit" name="submit" value="BEER TAKEN" />

PHP

$con=mysql_connect("localhost","user","pass") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

$search = mysql_query("SELECT * FROM members WHERE name LIKE '%$find%'") or die(mysql_error());

while($result = mysql_fetch_array( $search)) {
  echo $result['id'];
  echo $result['name'];
  echo $result['attendance'];
}

// The part what doesnt work:
if(isset($_POST['submit'])){
  mysqli_query($con, "UPDATE members SET attendance=1 WHERE id='%$result%['id']'");
}

Please help out!

Upvotes: 0

Views: 696

Answers (3)

Arun
Arun

Reputation: 105

mysql_select_db("members",$con);

U have to pass your connection variable along with mysql_select_db.

Try this code

<?php
$con=mysql_connect("localhost","user","pass") or die(mysql_error());
mysql_select_db("members",$con) or die(mysql_error());

$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

$search = mysql_query("SELECT * FROM members WHERE name LIKE '%$find%'") or die(mysql_error());

while($result = mysql_fetch_array( $search)) {
  echo $result['id'];
  echo $result['name'];
  echo $result['attendance'];
}
  echo '<form name="subform" action="<samefilename>.php" method="POST">  
        <input type="hidden" name="id" value='.$result['id'].'
        <input type="submit  name="submit" value="submit">
        </form>';


// The part what doesnt work:
if(isset($_POST['submit'])){
  mysqli_query($con, "UPDATE members SET attendance=1 WHERE id=".$result['id'].");
}
?>

Upvotes: 0

BitParser
BitParser

Reputation: 3968

Try the following:

mysqli_query($con, "UPDATE members SET attendance=1 WHERE id='".$result['id']."'"); 

Those redundant '%' are causing the trouble.

Upvotes: 2

Eisa Adil
Eisa Adil

Reputation: 1733

Use mysql_error() to find out the exact error of your code.

http://php.net/mysql_error

And just as a tip, try to declare %$result%['id'] as a variable and then use it in the query. it can result in readable clean code.

Upvotes: 0

Related Questions