I am trying to move the records from table studentrecords to passivestudents but i am not being able to. so far i've tried.
<?php
$db = new mysqli("localhost", "root", "","learndb");
if ($db->connect_error) {
die("Connection failed this is the error: " . $db->connect_error);
}
$id=$_GET['id'];
$sql="INSERT INTO passivestudents VALUES (DELETE FROM studentrecords WHERE id=$id)";
$result=$db->query($sql);
if(!$result)
{
echo"ERROR MOVING";
}
else
{
echo "<center><p style=\"color:green\">Information Moved!</p></center>";
}
?>
your query was wrong . you want to remove student with some id ? true ?
so follow these step:
1-INSERT INTO your_sec_table (select * from first_Table where id= $id;
2-DELETE FROM first_table WHERE id=$id
Related
I have problem with inserting my data into database
usually if something wrong it's showing error but this one not showing error (I don't know if this count as error or not cause I'm still new with this php coding stuff)
<?php
/*proses*/
$conn = mysqli_connect('localhost','root', '','dsolo');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$id = $_POST['id'];
$bulan = $_POST['bulan'];
$gajipokok = $_POST['gajipokok'];
$totalbonus = $_POST['totalbonus'];
$potongan = $_POST['potongan'];
$totalgaji = $_POST['totalgaji'];
$pajak = $_POST['pajak'];
$tes="INSERT INTO gaji set id='$id',
bulan='$bulan',
gajipokok='$gajipokok',
totalbonus='$totalbonus',
potongan='$potongan',
totalgaji='$totalgaji',
pajak='$pajak',";
mysqli_query ($conn,$tes) or die ($tes);
?>
After I press save what is showing is this one instead
INSERT INTO gaji set id='yuki', bulan='2019-08', gajipokok='2000000', totalbonus='2000000', potongan='1000000', totalgaji='3000000', pajak='300000',
What I need to change in my code?
There is an extra , in your query, you can remove that and your code look like the below code..
<?php
$conn = mysqli_connect('localhost','root', '','dsolo');
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
$id = $_POST['id'];
$bulan = $_POST['bulan'];
$gajipokok = $_POST['gajipokok'];
$totalbonus = $_POST['totalbonus'];
$potongan = $_POST['potongan'];
$totalgaji = $_POST['totalgaji'];
$pajak = $_POST['pajak'];
$tes="INSERT INTO gaji set id='$id', bulan='$bulan', gajipokok='$gajipokok', totalbonus='$totalbonus',potongan='$potongan', totalgaji='$totalgaji', pajak='$pajak'";
mysqli_query ($conn,$tes) or die ($tes);
}
?>
I am trying to fetch a value from one of my tables, however it doesn't appear to be retrieving it as when I echo the variable nothing is displayed.
Here is my code
<?php
$conn = new mysqli('localhost','root','root','neadatabase'); //Creating
connection to database
//Connection Error Checking
if ($conn->connect_error)
{
die("Connection Failed: " . $conn->connect_error);
}
echo ("Connection Successful");
//selecting bookings
$sqlSelectBookings = "
SELECT ActivID, DateRequired, TimeRequired
FROM activbooking
WHERE ActivID = '$_POST[ActivID]' AND DateRequired =
'$_POST[DateRequired]' AND '$_POST[DateRequired]' AND 'TimeRequired = '
$_POST[TimeRequired]'
";
$sqlSelectBookings = $conn->que($sqlSelectBookings);
//getting member limit
$sqlGetMemberLimit = "
SELECT memberLimit
FROM activity
WHERE activID = '$_POST[ActivID]'
";
$sqlGetMemberLimit = ($conn->query($sqlGetMemberLimit)-
>fetchObject(memberLimit));
echo $sqlGetMemberLimit;
Any ideas?
This is what happens when I run the page
Thanks
i have been trying since yesterday, and almost covered all questions regarding this matter in Stackoverflow plus googling, but so far nothing is working with me, i try to check username availability before updating the username in database, however, it wont check and always update the username directly without error message regarding not availability of the name..
here my code
//new connection
$con = new mysqli("localhost", "student", "student", "C14D5");
if ($con->connect_errno) { //failed
echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
}
//success
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['clientN'])) {
$query = mysqli_query("SELECT client_name FROM clients WHERE client_name='".$_POST['clientN']."'");
if (mysqli_num_rows($query) != 0) {
echo "<script>
alert('Username is not available, please select another username.');
</script>";
header('Location: '. $_SERVER['HTTP_REFERER'] );
} else {
// run sql
$sql ="UPDATE `clients` SET `client_name` = '".$_POST['clientN']."' WHERE `client_ID` = '".$_POST['SelectClient']."'";
if ($con->query($sql) === TRUE) {
echo "<h3> New record created successfully</h3>";
header('Location: '. $_SERVER['HTTP_REFERER'] );
} else {
echo "Error : " . $sql . "<br>" . $con->error;
}
$con->close();
}
}
You can use the mysqli_num_rows() function to avoid data duplication in your database
use this code :
//specify the database connection factors as usual ,then
$uname = $_POST['your_username_field'];
$sql = "SELECT * FROM your_db where username='$uname'";
//the variable 'sql' will store the resultset of the query
$num_row = mysqli_num_rows($sql);
// the 'num_row' will store the number of rows which matches your $sql resultset. So if it is greater than '0' then the data already exists
if( $num_row > 0)
{
// display 'username exists error'
}
else
{
// Insert user name into your database table
}
If the num_rows is greater than 0 ,then the username is already present in your database table . So at that case throw error. else INSERT the user name into your database and display success message .
I have 2 PHP pages to delete employee data from table. For that, user inserts employee id, and press delete, to delete data from table.
Now, problem is, whenever I inserts id of one digit(2,3,8 etc), id is not deleted. However, if two digit id is inserted (12,19,99 etc), it gets deleted.
Please help me to solve where I am wrong.
Here is my code for first PHP page:
<form action="deleteemp.php" method="post" onSubmit="return confirm('Are you sure to delete?')">
Enter id to delete data<input type="text" name="EmpId" required>
<button type="submit" >Delete</button>
</form>
Here is my action PHP page,
<?php
$EmpId = $_POST['EmpId'];
$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
die("Connection failed " . mysql_error());
}
$db_conn = mysql_select_db("hms", $connection);
if (!$db_conn) {
die("Connection failed " . mysql_error());
}
$query = "DELETE FROM employee_details WHERE emp_id = " . $EmpId;
$db_result = mysql_query($query, $connection);
if ($db_result) {
echo "Data Deleted Successfully !";
echo "<br>";
echo "<a href='homepage.php'>Back to homepage</a>";
} else {
echo "Data Not there. Try Again !<br>";
echo "<a href='deleteemp1.php'>Search again</a>";
}
echo "data not here" is incorrect. mysql_query returns boolean false on FAILURE. An empty result (no matching IDs) is NOT a failure. It's a successful query which happens to have an empty result set.
Your code should be more like
$result = mysql_query($query) or die(mysql_error());
if (mysql_affected_rows($result) == 0) {
die("No rows deleted");
}
And note that you are vulnerable to sql injection attacks, and using an obsolete/deprecated DB library.
Try this
$query = "DELETE FROM employee_details WHERE emp_id = '$EmpId'";
$db_result = mysql_query($query, $connection);
if ($db_result)
{
echo "Data Deleted Successfully !";
echo "<br>";
echo "<a href='homepage.php'>Back to homepage</a>";
}
else
{
echo "Data Not there. Try Again !<br>";
echo "<a href='deleteemp1.php'>Search again</a>";
}
This seems some exceptional issue, so try typecasting before passing value to SQL query.
Try using this for assigning value to $EmpId:
$EmpId = (int) $_POST['EmpId'];
can you try to change below code from
$query = "DELETE FROM employee_details WHERE emp_id = " . $EmpId;
TO
$query = "DELETE FROM employee_details WHERE emp_id =".$EmpId;
Just try. This might work for you
I have a database with 4 tables. (products,purchase,customer,user). When I tried to display all rows in products, no results. But in the user table, it displays. What should be the problem? Is it on my database?tables?php code?
Here's my code:
<?php
$db = mysqli_connect("localhost","root","", "prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sql = mysqli_query($db, "select * from user");
if( $sql === FALSE ) {
die('Query failed returning error: '. mysqli_error());
} else {
while($row=mysqli_fetch_array($sql))
{
echo $row['username']. "<br>";
}
}
?>
Hope you could help me.
Did you check the letters uppercase and lowercase in table columns?
You have inventory in your code, but in your question you named the table products, Is it this simple?
echo $row['item']. "<br>";
What is 'item'? Shouldn't you be selecting them by $row[0] or $row['products'].. or one of your other columns.
check your variables, for your example, try renaming your vairable $sql to something else, because you might have a similar variable somewhere in your code that you did not show.
try this :
<?php
$db = mysqli_connect("localhost","root","", "prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sqlstackoverflow = mysqli_query($db, "select * from user");
if($sqlstackoverflow === FALSE ) {
die('Query failed returning error: '. mysqli_error());
} else {
while($row=mysqli_fetch_array($sqlstackoverflow))
{
echo $row['username']. "<br>";
}
}
?>
Your code references an inventory table but you said your database has a products table (and no inventory table). Because of this, the query is failing.
Just Try With The Following :
<?php
$db = mysqli_connect("localhost","root","","prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sql = mysqli_query($db,"select * from user");
if( $sql === FALSE ) {
die('Query failed returning error: '. mysqli_error());
} else {
while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC))
{
echo $row['username']."<br>";
}
}
?>
I think this may help you to resolve your problem.