jpergega Posted March 18, 2014 Share Posted March 18, 2014 Hi I have two different types of users one is student the other is staff. Right now in the staff account the staff can search student from the database. What I want to do is that when the search result returns the staff can click on the add button to store copy of this student's data to another database table. Right now I am not able to add copy of the student record into another table because: 1.there is the foreign key constraint 2.I know the code in the data.php(see below) is incorrect. I would grateful if you could point out what I need to do? Thanks Here is the code in the Staff Account; <?php if(isset($_POST['find'])){ if(empty($_POST['find'])){ $nameErr = "Missing";} else{ //$find = $_POST['find']; $results=getStudent(); echo "<table> <tr> <th>Username</th> <th>First Name</th> <th>Surname</th> </tr>"; while ($row = mysql_fetch_array($results)){ echo "<tr>"; echo "<form action=data.php method=post>"; echo "<td>" .$row['username']."</td>"; echo "<td>".$row['firstname']."</td>"; echo "<td>".$row['surname']."</td>"; echo "<input id=addS name=adds type=submit/></form></td>"; echo "<tr/>"; } echo"</table>"; } } ?> Here is the code in the data.php <?php if(isset($_POST['adds'])){ $_row['username'] = $_POST['username']; $_row['firstname'] = $_POST['firstname'] ; $_row['surname'] = $_POST['surname'] ; //inserting data order $order = "INSERT INTO studentgroup (username,firstname,surname) VALUES ('$_POST[username]', '$_POST[firstname]','$_POST[surname]')"; $result = "select * from studentgroup"; if(!mysqli_query($con,$order)){ die('Error:'. mysqli_error($con)); } header ("Location: staffAccount.php"); } } ?> Link to comment Share on other sites More sharing options...
Ingolme Posted March 18, 2014 Share Posted March 18, 2014 You can copy values from one table to another using INSERT INTO SELECT. You should never pass $_POST, $_GET, $_REQUEST or $_COOKIE values directly into a query. It can allow for SQL injection in which a user can manipulate your database and pretty much hack your website. In order to prevent this use mysqli_real_escape_string() or mysqli_prepare(). Link to comment Share on other sites More sharing options...
jpergega Posted March 18, 2014 Author Share Posted March 18, 2014 Thanks for the reply Right now the problem is: Error:Cannot add or update a child row: a foreign key constraint fails because I logged in as a staff account and I am copying an existing student account detail into another table what do I need to change in the database table then? Right now both users' details are store in one table called userAccount and there is permission setting in the table to distinguish the two. Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now