How can I check if a MySql field is equal to 1? - php

I'm creating a forum in PHP and using MySql as a database, and was wondering how I could check if a MySql field topic_locked was equal to 1. If it isn't, the reply code would be displayed. How can I check this, and if you can help me find how to check this, how could I set it to 1 through the forum?

I dont know your code. But I am sharing simple program to check a mysql field's value.
<?php
// Database select and connect to host
$sql= mysql_query("SELECT topic_locked FROM table WHERE Id='your_id'");
$res= mysql_fetch_array($sql);
$value= $res['topic_locked'];
if($value=='1')
{
// reply code
}
?>
Update asked,
<?php
if(isset($_POST['update']))
{
$id= $_POST['id'];
//Database select and connect to host
mysql_query("UPDATE table SET topic_locked='1' WHERE Id='$id'");
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="your_id" />
<input type="submit" name="update" />
</form>

Related

If input exists from one table update it on another one

Im trying to make like a refill code that will refill my table.
I have one table that have my refill code in it and other table that stores the balance of the account.
table1: card_credit (table that stores the balance of the account)
table2:card_refill (table that have me refill code)
I have created this code with session and PHP. Now I'm stuck and dont know how to move forward.
I want to make when i write in the refill code from table card_refill that its take the amount of credit into value in table card_refill
refill.php
<strong>Refill</strong>
<form action="refill.php" method"post">
<input type="text" name="refillcode"/>
<br>
<input type="submit" name="Submit" value="Refill" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['refillcode'] = $_POST['refillcode'];
}
?>
Here is a possible solution, I am just don't know, where the card_id comes from.
This is inserting a new record into your card_credit table.
// starting the session before any output
session_start();
//include here the database connection file!
//for example:
//include('db_connection.php');
if (isset($_POST['Submit'])) {
//First do a validation here, is the refillcode number, exists, etc...
//Insert it into the table
$sql = "INSERT INTO card_credit (card_id, value) VALUES ('[YOUR_CARD_ID_HERE]', " . intval($_POST['refillcode']) . ")";
//Link is the resource variable when you created the mysqli_connect
mysqli_query($link, $sql);
//Redirect here if you want
}
?>
<!-- HTML CODE STARTS HERE -->
<strong>Refill</strong>
<form action="refill.php" method="post">
<input type="text" name="refillcode"/>
<br>
<input type="submit" name="Submit" value="Refill" />
</form>

How do I add a row to mySQL using PHP?

I am trying to add an "admin" section of my website. Right now I am working on a section to add a new row to my MySQL database.
The first file is my admin.php:
<html>
...
<body>
<form action="add.php" method="post">
<input type="text" name="order" />
<input type="text" name="newstatus" />
<input type="submit" value="Add" />
</form>
</body>
</html>
My goal here is to add 2 pieces of data (the table only has 2 columns right now) to the new row.
Then, I have my add.php file:
<?
//declare my connection variables - I'll move these to a secure method later
mysql_connect($servername, $username, $password) or die (mysql_error ());
// Select database
mysql_select_db($dbname) or die(mysql_error());
// The SQL statement is built
$sql="INSERT INTO $tblname(id, status)VALUES('$order', '$newstatus')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='admin.php'>Back to main page</a>";
}
else {
echo mysqli_errno($this->db_link);
}
?>
<?php
// close connection
mysql_close();
?>
Anytime i input any data (non-duplicate of what is already in the table), it gives me an error. If I clear my table completely of all data, it will input once. I am getting a duplicate key error, but my key should be "orders", which is unique every time I input it.
Suggestions?
If you are actually inserting a new row, you shouldn't fill the ID yourself but rather set it as AUTO_INCREMENT in your database. And then, have you form as such:
<form action="add.php" method="post">
<input type="text" name="newstatus" />
<input type="submit" value="Add" />
</form>
And your PHP code like so:
$newstatus = mysql_real_escape_string($_POST['newstatus']); // note the usage of $_POST variable
$sql="INSERT INTO `$tbl_name` (`status`) VALUES ('$newstatus')";
$result = mysql_query($sql) or die('Failed executing query: '.mysql_error());
AUTO_INCREMENT can be set up in phpMyAdmin with the following query:
ALTER TABLE `WorkOrders` MODIFY `id` INTEGER NOT NULL AUTO_INCREMENT;
Finally, don't use mysql_* functions, they are deprecated.
I am guessing the 'id' field of your table is a primary key. If that's the case, you cannot have two rows that have the same identifier.
By your variable name newstatus, it seems to me like you're trying to update the status of an order ; is it the case? If yes, you should use a UPDATE SQL query of the form:
UPDATE table SET status='somestatus' WHERE id=someid

Deleting Information From Database String Input

I am looking to delete a particular row from a database using the code below. The code below is within a file called "delete.php" and it is taking input from an input box that is located on another php file called "yourReports.php".
When the form is submitted on "yourReports.php" it should delete the row from the database, however it doesn't appear to be working.
delete.php
<?php
$mysqli = mysqli_connect("localhost", "root", "DBPASS","DBNAME") or die ('Could not connect to database!');
$_POST['delete'];
$deletereport = mysqli_real_escape_string($mysqli, $_POST['delete']);
mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '".$deletereport."'");
header('Location: yourreports.php');
?>
yourReports.php
<form action="delete.php" method="post" name="form1">
<label><strong>Enter Report Name To Delete:</strong></label>
<input name="delete" id="delete" type="text">
<input value="Delete Report" name="delete" class='myButton' type="submit">
</form>
Any help would be appreciated.
Thanks
Set the method and name attribute of form to post
<form action="delete.php" method="post" name="form1">
Check if the query fails:
if(! mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '".$deletereport."'")){
echo mysqli_error();
}
[Updated]
You cannot have two inputs with the same name. Your input and delete button both have the name delete. So try considering different names for inputs.
change
mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '".$deletereport."'");
To
mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '{$deletereport}'");

Call to DB to check if a value exists

I'm trying to run a query to check if what is entered into a textfield matches that of what is stored in a database.
The current code:
</form>
<br />
<h2>Discount Code</h2>
<br />
<form method="POST" action=''>
<input type="text" name="discount" />
<input type="submit" name="discountSubmit" value="Apply" />
<?php
if(isset($_POST['discountSubmit'])){
$discountCode = $_POST['discount'];
$codeCheck = mysqli_query("SELECT code FROM discount WHERE code = $discountCode");
var_dump($codeCheck);
}
?>
</form>
However, upon clicking discountSubmit the var_dump returns NULL so it would lead me to assume $codeCheck is wrong, however it looks right to me.
I connect through the database through another page so the issue doesnt lie there
Database Structure:
id code discount expire
1 WEB10 10.00 2013-06-01
Expire isn't relevant just at the moment.
Here, code is not an integer value. Hence, enclose it by single quotes. You should also include the $conn (connection variable) while using mysqli_query() statement - mysqli_query()
$codeCheck = mysqli_query($conn, "SELECT code FROM discount WHERE code = '$discountCode'");
[EDIT]
If you are including the connection page inside this page, try doing these:
connect.php
function connect(){
$conn = mysqli_connect($host, $un, $pw, $db);
return $conn;
}
Now, call this function from the current page and get the connection variable:
$conn = connect();
Now, use $conn for the mysqli_query() function.
Change the if condition as follows:
if(isset($_POST['discount']))
You shouldn't mix SQL with HTML
You shouldn't use a function without reading its manual page first
You shouldn't use bare API with mysqli, but some abstraction library instead.
A better version for your code, courtesy of safeMysql which works the natural way you expect from mysqli_query but don't get it.
<?php
$check = NULL;
if(isset($_POST['discountSubmit']))
{
$sql = "SELECT code FROM discount WHERE code = ?i";
$check = $db->getOne($sql, $_POST['discount']);
}
?>
<h2>Discount Code</h2>
<br />
<form method="POST" action=''>
<input type="text" name="discount" />
<input type="submit" name="discountSubmit" value="Apply" />
</form>
<?php var_dump($check); ?>

DELETE FROM table WHERE ID='$id' — Variable refuses to stick

Trying to perform a very simple task here.
I have an <ol> that contains 4 rows of data in some handy <li>s. I want to add a delete button to remove the row from the table. The script in delete.php appears to have finished, but the row is never removed when I go back and check dashboard.php and PHPMyAdmin for the listing.
Here's the code for the delete button (inside PHP):
Print "<form action=delete.php method=POST><input name=".$info['ID']." type=hidden><input type=submit name=submit value=Remove></form>";
Moving on to delete.php:
<?
//initilize PHP
if($_POST['submit']) //If submit is hit
{
//then connect as user
//change user and password to your mySQL name and password
mysql_connect("mysql.***.com","***","***") or die(mysql_error());
//select which database you want to edit
mysql_select_db("shpdb") or die(mysql_error());
//convert all the posts to variables:
$id = $_POST['ID'];
$result=mysql_query("DELETE FROM savannah WHERE ID='$id'") or die(mysql_error());
//confirm
echo "Patient removed. <a href=dashboard.php>Return to Dashboard</a>";
}
?>
Database is: shpdb
Table is: savannah
Ideas?
It's refusing to stick because you're calling it one thing and getting it with another. Change:
"<input name=".$info['ID']." type=hidden>"
to
"<input name=ID value=".$info['ID']." type=hidden>"
because in delete.php you're trying to access it with:
$id = $_POST['ID'];
You should really quote attribute values as well ie:
print <<<END
form action="delete.php" method="post">
<input type="hidden" name="ID" value="$info[ID]">
<input type="submit" name="submit" value="Remove">
</form>
END;
or even:
?>
form action="delete.php" method="post">
<input type="hidden" name="ID" value="<?php echo $info['ID'] ?>">
<input type="submit" name="submit" value="Remove">
</form>
<?
Please, for the love of the web, don't built an SQL query yourself. Use PDO.
Just another point I'd like to make. I'm 95% sure that you can't give an input a numeric name/id attribute. It has to be like "id_1" not "1".
Also with php you can do arrays.
So you could do this
<input name="delete[2]">
then in your php
if(isset($_POST['delete']))
foreach($_POST['delete'] as $key=>$val)
if($_POST['delete'][$key]) delete from table where id = $val

Categories