php/mysql transaction
I'm doing a transaction with PHP and MySQL. Using PHPMyAdmin I'm inserting queries into my University DB, where I'm supposed to use transactions in some tables. So far I've made this code for my Staff transactions, but my problem is how can I get the information inserted in addStaff.php so I can use it as a query on this code? right where it says //values();
<?php
function begin()
{
mysql_query("BEGIN");
}
function commit()
{
mysql_query("COMMIT");
}
funct开发者_如何学JAVAion rollback()
{
mysql_query("ROLLBACK");
}
mysql_connect("localhost","username", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$query = "INSERT INTO Staff (id,name,position,phone,email,roomNumber,dnumber)"
//values();
begin(); // BEGIN
$result = mysql_query($query);
if(!$result)
{
rollback(); // ROLLBACK
echo "You rolled back";
exit;
}
else
{
commit(); // COMMIT
echo "Transaction was succesful";
}
?>
This is maybe what you're looking for:
$new_row = mysql_insert_id();
$query = mysql_query("SELECT * FROM `Staff` WHERE `id`=".$new_row);
$r = mysql_fetch_assoc($query);
echo $r['name'];
will echo the inserted rows name.
Edit: This is a very very basic version of how to do things, before moving anything to production you need to read up on SQL Injection, Prepared Statements/Escaping User Input, XSS Attacks and many more vital parts of SQL query security
If I understand you question correct, you need to know how to prompt for data, accept it, and insert it into the database:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... connect to the database ...
$sometext = $_POST['textfield']; // retrieve the value from the form
$qsometext = mysql_real_escape_string($sometext); // make it safe for the query
$sql = "INSERT INTO mytable (textfield) VALUES ($qsometext);" // build the sql query
$result = mysql_query($sql) or die(mysql_error()); // run the query
}
?>
<html>
<body>
<form method="POST">
<input type="text" name="textfield"><input type="submit">
</form>
</body>
</html>
That's a barebones version of how to show a form, then insert the user's data into a database, the simply re-displays the form for more data.
精彩评论