Update database using mySQL & php PDO - mysql

I've created a database for one of my classes simulating a hotel reservation form.
my database table name that I am trying to get values from is tblNameRes and the fields are fldName and pkEmail
I have gotten the values from the database and displayed them in this table here:
http://www.uvm.edu/~cchessia/cs148/assign4/strugg.php
I want to add a column to be able to update and delete these records, but I don't really understand how. I have this code:
$updating = $db->prepare('UPDATE `tblNameRes` SET `name` = `name` + ? WHERE `id` = ?');
$updating->execute(array(20, $id));
but I want to be able to click a link that says "update" and it will take me to another page that allows me to edit the data and submit it back to the database. I would also like to be able to delete the data in the same way.

I'm a little confused to what you would like, would you like the PDO code to both update and delete items from your DB? If so I use this, I'm also new to PDO. Also try not to use ' around table/field names.
/** Code to update */
$query = "UPDATE tblNameRes SET name ='$name' WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->BindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
/** Code to delete */
$query = "DELETE FROM tblNameRes WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->BindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
Let me know if this helps, or is not what you wanted. I'm new here so I can't post 'comments'.

/* code for update and remove this 'at the beginning an at the end*/
$query = "UPDATE tblNameRes SET name =:fname WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->BindValue('fname',$fname);
$stmt->execute();

Related

Cascade delete in many-to-many tables

Assume a set of mailing lists in which individuals may be members of more than one list. I've set up a join table:
members -> members2lists <- lists
If the user wants to remove a member from one list only, it would seem necessary only to delete the appropriate row in the members2lists table. But how do I specify the cascading so as not to leave them as an orphan if they're a member of only one list? In other words, how do I delete a member if and only if they are a member of solely the list I'm removing them from?
I'm using PHP and mySQL with InnoDB tables.
Thanks David for your response. I've solved the problem this way (I hope the first two functions are self-expanatory):
$memberID = getIDFromMembers($pdo, $email);
$currListID = getIDFromList($pdo, $listname);
// remove record from join table
$sql = 'DELETE FROM `members2lists` WHERE `member_fk` = :member AND `list_fk` = :list';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':member', $memberID);
$stmt->bindParam(':list', $currListID);
$stmt->execute();
// check if another record exists in the join table for the same member
$sql = 'SELECT `member_fk` FROM `members2lists` WHERE `member_fk` = :member';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':member', $memberID);
$stmt->execute();
$row = $stmt->fetch();
if($row === false)
{
// there isn't, the member is an orphan, so delete
$sql = 'DELETE FROM `members` WHERE `member_email` = :email';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':email', $email);
$stmt->execute();
}
I'd be glad of comments from members about ways to improve this code!

How to INSERT an entry without content into a table in ZF2?

In my database there is a table, that contains only one row: id. It's something like a facade for another table:
Now I want to add an entry into whatever_set, in order to be able to create entries in the dependent tables. In SQL I would write:
INSERT INTO whatever_set VALUES ();
But when I try it with Zend\Db
$action = new Insert(whatever_set');
$data = [];
$action->values($data);
$sql = new Sql($this->dbAdapter);
$statement = $sql->prepareStatementForSqlObject($action);
$result = $statement->execute();
I get an exception:
values or select should be present
There are some possible workarounds (see below). But is there though a "Zend way" to save empty with no VALUES? How to do this?
Workaround 1
Custom Insert class.
Workaround 2
Raw SQL like
$sql = 'INSERT INTO whatever_set VALUES ();';
$result = $this->dbAdapter->getDriver()->getConnection()->execute($sql);

mysql: get affected row record after update

I recently use two sql queries to get row record after update like this, for example:
Step 1: I update point for user test#gmail.com
$sql = "UPDATE user SET point=point-:point WHERE email=:email";
$result = $conn->prepare($sql);
$result->bindValue(':point', 2);
$result->bindValue(':email', 'test#gmail.com');
$result->execute();
Step 2: I have to use another sql query to get row['point'] after it is updated
$sql = "SELECT point FROM user WHERE email=:email";
$result = $conn->prepare($sql);
$result->bindValue(':email', 'test#gmail.com');
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
echo $row['point'];
So, my question is, is ther a shortcut for this? such as 1 single query to achieve this?
Many thanks

PDO prepared statement with insertion in database

Please i have this quetion,
I have here:
$query = $db->prepare('Update survey_content set '.$type.' = '.$type.'+1 where id = '.$where);
$query->execute();
I'm new to PDO and i really don't know how to create a new query, i need to add a record on the database, is it ok like this?
$query1 = $db->prepare('INSERT INTO daily (selected)
VALUES (.$type.)');
$query1->execute();
Use a placeholder and fill the value in the execute method
$query1 = $db->prepare("INSERT INTO daily (selected)
VALUES (?)");
$query1->execute($selected_data);
or bind the parameter seperately
$query1 = $db->prepare("INSERT INTO daily (selected)
VALUES (:sel_dat)");
$query1->bindParam(":sel_dat",$selected_data);
$query1->execute();

SET operator use in mysql. Explanation?

Allright, i see that without 1st line, the second query will not work. But I don't understand why. Why its not working without SET? What set does mean ?
$query = "update messages set $name=$name+1 where id='$idd'";
$db->setQuery( $query );
$db->query( $query ) or die('blogai');
$query2 = "select up,down from messages where id='$idd'";
$db->setQuery( $query2 );
$db->query( $query2 ) or die('blogai');
Check your sql request in your database query editor :
select up,down from messages where id='yourValue'