doucheng7808
2011-05-12 19:37
浏览 50
已采纳

php / mysql事务

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");
}
function 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";
}
?>

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHP和MySQL进行交易。 使用PHPMyAdmin我将查询插入到我的大学数据库中,我应该在某些表中使用事务。 到目前为止,我已经为我的员工交易制作了这个代码,但我的问题是如何在addStaff.php中插入信息,以便我可以将其用作此代码的查询? 正确的地方是 // values();

 &lt;?php 
function begin()
 {
 
 \ mysql_query(“BEGIN”  ); 
} 
 
函数commit()
 
 
 
 
 \ {n mysql_query(“COMMIT”); 
} 
 
函数rollback()
 
 
 
 
 
 \ {
 
 
 
 
 
 
 
 \ mysql_query(“ROLLBACK”); 
} 
 
mysql_connect(  “localhost”,“username”,“password”)或die(mysql_error()); 
mysql_select_db(“test”)或die(mysql_error()); 
 $ query =“INSERT INTO Staff(id,name,position  ,电话,电子邮件,roomNumber,dnumber)“
 //值(); 
 
begin();  // BEGIN 
 $ result = mysql_query($ query); 
if(!$ result)
 
 \ {
 
 
 
 
回滚(
);  // ROLLBACK 
 echo“你回滚”; 
退出; 
} 
else 
 {
 commit();  // COMMIT 
 echo“Transaction is successful”; 
} 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duan1983 2011-05-12 20:21
    已采纳

    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.

    点赞 评论
  • doudao2407 2011-05-12 20:21

    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

    点赞 评论

相关推荐 更多相似问题