amira90 Posted May 1, 2013 Share Posted May 1, 2013 i'm trying to add data into specific user in database. the one who login in the form is admin and the admin will add information to the user's database.. here's the php code <?php session_start(); if ($_SESSION['UserId'] == "") { echo "Please Login first"; } else { echo "<h1> Appliances's adding list</h1>"; echo "<table>"; echo "<form action ='addappliancesprocess.php' method = 'POST'>"; echo "<tr> <td>Appliances Name: <input type='text' name='appliancesname'></td> </tr>"; echo "<tr> <td>Port Number: <select name = 'port' value='port'> <option value 'RB0'>RB0</option> <option value 'RB1'>RB1</option> <option value 'RB2'>RB2</option> <option value 'RB3'>RB3</option> <option value 'RB4'>RB4</option> <option value 'RB5'>RB5</option> <option value 'RB6'>RB6</option> <option value 'RB7'>RB7</option> </select> </tr>"; echo "<tr> <td>Insert characters on: <input type='text' name='charon'></td> </tr>"; echo "<tr> <td>Insert characters off: <input type='text' name='charoff'></td> </tr>"; echo "<tr> <td colspan=2 align= 'right'><br> <INPUT Type='button' VALUE='Back' onClick='history.go(- 1);return true;'> <input type='submit' name='update' value='Submit'> </td> </tr>"; echo "</table>"; } ?> and here is the process <?php session_start(); $appliancesname=$_POST['appliancesname']; $port=$_POST['port']; $username=$_REQUEST['username']; $charon=$_POST['charon']; $charoff=$_POST['charoff']; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("myfyp", $con); ?> <?php $query="SELECT from user where UserId = '".$_SESSION ['UserId']."'"; //if ($username != UserId) { //echo "no user"; //}else{ $sql="INSERT INTO appliances VALUES ('','$appliancesname','$port','$username','','$charon','$charoff')"; echo $query; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "You have choose the following:<br>"; echo "<br>Appliances: $appliancesname</br>"; echo "<br>Port Number: $port</br>"; echo "<br>Character on: $charon</br>"; echo "<br>Character off: $charoff</br>"; echo "<br><INPUT Type='button' VALUE='Back' onClick='history.go(- 1);return true;'>"; //} ?> when i try to add it will add to the database but the port is not updated and the userid is empty.. i'm sorry.. i am new to php.. there are lots that i don't know.. Quote Link to comment https://forums.phpfreaks.com/topic/277500-add-data-in-database-to-a-specific-user/ Share on other sites More sharing options...
lemmin Posted May 1, 2013 Share Posted May 1, 2013 $_REQUEST['username'] does not appear to be set and your HTML for the options' values is missing the equal signs: <option value 'RB0'>RB0</option> Needs to be: <option value='RB0'>RB0</option> Quote Link to comment https://forums.phpfreaks.com/topic/277500-add-data-in-database-to-a-specific-user/#findComment-1427571 Share on other sites More sharing options...
amira90 Posted May 1, 2013 Author Share Posted May 1, 2013 (edited) Thank you.. Thank you for your help.. the port is already updated but the userID field is still empty.. my database look like this.. btw the AppliancesID should be increment but it also empty.. Edited May 1, 2013 by amira90 Quote Link to comment https://forums.phpfreaks.com/topic/277500-add-data-in-database-to-a-specific-user/#findComment-1427655 Share on other sites More sharing options...
lemmin Posted May 1, 2013 Share Posted May 1, 2013 I'm assuming that the UserId field is right after the username field, in which case, change your VALUES list to this: ('','$appliancesname','$port','$username','".$_SESSION['UserId']."','$charon','$charoff')"; Quote Link to comment https://forums.phpfreaks.com/topic/277500-add-data-in-database-to-a-specific-user/#findComment-1427656 Share on other sites More sharing options...
amira90 Posted May 1, 2013 Author Share Posted May 1, 2013 its says like this.. SELECT from user where UserId = 'admin'Error: Duplicate entry '' for key 'PRIMARY' i'm sorry.. my english is not good.. but is it possible for admin(who is the one who login) to add data to the user? i want the query to be like SELECT from user where UserId = 'user' and i think because i use session so the query will always turned to be SELECT from user where UserId = 'admin' Quote Link to comment https://forums.phpfreaks.com/topic/277500-add-data-in-database-to-a-specific-user/#findComment-1427659 Share on other sites More sharing options...
lemmin Posted May 1, 2013 Share Posted May 1, 2013 You are never using your SELECT query. You are just echoing it out and never running it. It would fail anyway, so you can just remove it. The query in question is the INSERT query. Duplicate entry means your are trying to insert an ID (I'm assuming AppliancesID is your primary key) that is already in the database. In your case, that duplicate is likely to be the empty id. Change the AppliancesID field to use the AUTO_INCREMENT flag. Quote Link to comment https://forums.phpfreaks.com/topic/277500-add-data-in-database-to-a-specific-user/#findComment-1427664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.