Page 2 of 2

Posted: Sun Dec 23, 2007 11:36 pm
by RobertGonzalez
This would work:

Code: Select all

$query = "SELECT * FROM table_name WHERE name='$where'";
And this would work:

Code: Select all

$query = "SELECT * FROM table_name WHERE name='".$where."'";
Of course, this would work as well:

Code: Select all

$query = 'SELECT * FROM table_name WHERE name=\''.$where.'\'';
No amount of throwing a double quote at the end of the string would work. Sorry. And I agree with feyd. If you are going to use double quotes to parse variables in a string, use them. Otherwise, concatenate single quoted strings.

Posted: Mon Dec 24, 2007 2:55 am
by seriousdamage
Hi this is all very interesting, but how about we return to my little problem? :D

Posted: Mon Dec 24, 2007 4:01 am
by RobertGonzalez
Your original problem is essentially asking for someone to write some code for you. I'd suggest you pick up a copy of Kevin Yank's Sitepoint book 'Build Your Own Database Driven Website Using PHP & MySQL'. Even the first four chapters (available as a sample) will be able to answer the question you are asking.

Posted: Mon Dec 24, 2007 4:09 am
by webspider
when the outcome is posted, is it posibble that beside every record I could have a button or a link to delete that specific record?
definitely yes :-). Create a link like http://localhost/delete.php?del=field_name. Capture the value of del in delete.php and execute mysql query to delete the record


this is search.php

Code: Select all

<?php
$link = mysql_connect('host_name', 'user', 'pass')
            or  die(mysql_error());

mysql_select_db('db_name');
$where = mysql_real_escape_string ($_POST['name']);

$query = 'SELECT * FROM table_name WHERE name=\''.$where.'\'';
 
$sql = mysql_query($query);

if (!$sql) {
    die('Invalid query: ' . mysql_error());
}


while ($row = mysql_fetch_object($sql)) {
  //print your data as you like
    echo $row->field_name1."  <a href=delete.php?del=$row->fieldname2>Delete</a> <br/>";
}
?>
and this is delete.php

Code: Select all

<?php
$link = mysql_connect('host', 'user', 'pass')
            or  die(mysql_error());

mysql_select_db('db_name');
$where = mysql_real_escape_string ($_POST['del']);

$query = 'DELETE FROM table_name WHERE fieldname2=\''.$where.'\'';
 
$sql = mysql_query($query);

if (!$sql) {
    die('Invalid query: ' . mysql_error());
}
else 
echo mysql_affected_rows()." row delete succesfully";


?>

I have not tested it.hopefully it'll works :)

Posted: Mon Dec 24, 2007 5:47 am
by seriousdamage
Hi, it keeps returning "0 row delete succesfully"
and the record still in the database.

Posted: Mon Dec 24, 2007 6:29 am
by webspider
make the change in delete.php

Code: Select all

$where = mysql_real_escape_string ($_REQUEST['del']);
$query = 'DELETE FROM table_name WHERE fieldname=\''.$where.'\'';

Posted: Mon Dec 24, 2007 7:03 am
by seriousdamage
Thanks, it workes great,
I think I just need one more function to finish,
this would be to modify a record within a form.

I have added a new link called modify just like you showed me for delete,
but what I am looking for is that when I click the record can become writsble so that I can change it and re-submit it into the database.

Can this be done?

Posted: Sat Dec 29, 2007 9:19 am
by webspider
hi seriousdamage,
i think you have done this already :) .and if not it could help you.

in search.php create another link for edit beside Delete link, like this

Code: Select all

echo "<a href=edit.php?id=".$row->field_primarykey."&name=".$row->fieldname.">Edit</a>";
create edit.php for edit the fieldname

Code: Select all

<?php

$link = mysql_connect('hostname', 'user', 'password')
            or  die(mysql_error());

mysql_select_db('databasename');

$where = mysql_real_escape_string ($_REQUEST['id']);
$what = mysql_real_escape_string ($_REQUEST['name']);

?>

<form name="form" action="update.php" method="post">
  <input type="text" name="name" value="<?php echo $what; ?>"/>
  <input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />
  <input type="submit" name="Submit" value="Edit" />
</form>

<?php
mysql_close($link);
?>
and finally update in the database -> update.php

Code: Select all

<?php

$link = mysql_connect('hostname', 'user', 'password')
            or  die(mysql_error());

mysql_select_db('databasename');
$where = mysql_real_escape_string ($_REQUEST['id']);
$what = mysql_real_escape_string ($_REQUEST['name']);



$query = 'UPDATE tb_address_book 
          SET fieldname=\''.$what.'\' WHERE id=\''.$where.'\'';
 
$sql = mysql_query($query);

if (!$sql) {
    die('Invalid query: ' . mysql_error());
}
else 
echo mysql_affected_rows()." update succesfully";

mysql_close($link);
?>