What is wrong in this php code? Retrieving data from database - php

What is wrong in this code? Here I am trying to show the data fetched from a mySQL database when someone clicks on the "Show" button (Which is a submit button).
<?php
if(isset($_POST['submit'])){
$mysqli = new mysqli("localhost", "rms", "sarangi", "rms");
if($mysqli === false){
die("Connection Error.");
}
else{
echo'<table border="1"><tr><th> Name </th><th> Username </th></tr>';
$sql = "SELECT * FROM users";
if($result = $mysqli->query($sql)){
if($result->num_rows > 0){
while($data = $result->fetch_array()){
echo'<tr><td>' . $data['firstname'] . " " . $data['lastname'] . '</td><td>' . $data['username'] . '</td></tr>';
}
}
}
echo'</table>';
}
$mysqli->close();
}
else{
?>
<form action="index.php" method="POST">
<input type="submit" id="submit" value="Show" />
</form>
<?php
}
?>

You have to name your input field
<input type="submit" id="submit" value="Show" />
should be
<input name="submit" type="submit" id="submit" value="Show" />

change your code to this
<form action="" method="POST">
<input type="submit" id="submit" name="submit" value="Show" />
</form>
I have changed form action to "" since its going to submit to the same page. or you can leave it as index.php if this page is index.php. You need to put the name attribute of input submit as submit

Related

php: getting a variable (user selection from radio button) to another file

I have a list of items in one file (fruit.php):
<form action="delete_items.php" method="post">
<input type="radio" value="apple" name="fruit">Apple<br>
<input type="radio" value="pear" name="fruit">Pear<br>
<input type="radio" value="banana" name="fruit">Banana<br>
<input type="submit" value="delete" name="deleteButton"><br>
</form>
In the delete_items.php, I have a variable that stores the selection:
<?php
$selection = $_POST["fruit"];
echo "are you sure you want to delete it?";
echo '<form action="delete_confirmation.php" method="post"><br>';
echo '<input type="submit" name="deleteYes" value="Yes"><br>';
echo '<input type="submit" name="deleteNo" value="No"><br>';
echo '</form>';
?>
Than in the confirmation file (delete_confirmation.php):
<?php
include 'delete_items.php';
if($_POST["deleteYes"]){
$query = 'DELETE FROM databaseName WHERE fruitName="' . $selection . '"';
$result = mysqli_query($connection, $query);
if(!$result){
die("Delete after check query failed!");
} else {
echo "Delete after check query success!";
}
} elseif($_POST["deleteNo"]){
echo "The course was not deleted!";
}
?>
But the varibale $selection in the delete_confirmation.php file is always null even though i included the delete_items.php file (when i echo it, nothing is shown). Is there a way to pass the selection variable from delete_items.php to the delete_confirmation.php file?

how to get the name tag of an image

i have an image from my database in a different table displayed as $draft['draft_image'] and i want to send this data to a different table but im having a problem cause this code doesnt seems to work.. when i tried to click the published button there is no data that is being sent.i use var_dump to show the inserted data but nothing shows up in the inspect element.. any idea how can i get the image? this is what's inside my draft_image.. ../images/article/md.png
<form method="POST" enctype="multipart/form-data">
<img data-name="title_image" src="./<?= $draft['draft_image'];?>" style="margin-top:5px;" width="70%" height="55%"><br><br>
<?php
}
?>
<button type="submit" name="submit" class="btn3 btn-default">Publish</button>
</form>
if (isset($_POST['submit'])) {
$id = $session_id;
$file = $_POST['title_image'];
$ress = $db->draftpublished($id,$file);
echo $ress;
}
?>
You can pass the value using hidden type ex:
<form method="POST" enctype="multipart/form-data">
<img data-name="title_image" src="./<?= $draft['draft_image'];?>" style="margin-top:5px;" width="70%" height="55%"><br><br>
<input type="hidden" name="imgsrc" value="<?= $draft['draft_image'];?>">
<?php
}
?>
<button type="submit" name="submit" class="btn3 btn-default">Publish</button>
</form>
if (isset($_POST['submit'])) {
$id = $session_id;
$file = $_POST['imgsrc'];
$ress = $db->draftpublished($id,$file);
echo $ress;
}
?>
<form method="POST" enctype="multipart/form-data">
<!-- added ----------------- -->
<input type="hidden" name="title_image" value="<?=$draft['draft_image']?>">
<img data-name="title_image" src="./<?= $draft['draft_image'];?>" style="margin-top:5px;" width="70%" height="55%"><br><br>
<?php
}
?>
<button type="submit" name="submit" class="btn3 btn-default">Publish</button>
</form>
if (isset($_POST['submit'])) {
$id = $session_id;
$file = $_POST['title_image'];
$ress = $db->draftpublished($id,$file);
echo $ress;
}
?>

How to rember $_POST var from previous <form method>?

if ($_SERVER['REQUEST_METHOD'] != 'POST'){
echo '<form method="post" action="">
<select name="class_name">
<option value="class_id">name</option>
</select>
<input type="submit" value="Proceed">
</form>';
}
else{
$_SESSION['var_name'] = $_POST['class_name'];
$sql = "SELECT (...) students.class_id = " . $_SESSION['var_name'];
$que = mysqli_query($conn, $sql);
echo '<table border 1>
<tr><th>Last</th><th>Name</th><th>Add</th></tr>';
while ($row = mysqli_fetch_array($que)){ //<-HERE I GOT A ERROR
echo '<tr><td>' . $row['user_last'] . '</td><td>' . $row['user_name'] . '</td>
<td> <form method="post" action="">
<input type="hidden" name="usr_id" value = "' . $row['user_id'] . '"/>
<input type="submit" name="button" value="+" />
</form>
</td>
</tr>';
}
echo '</table>';
if (isset($_POST['button'])){
//DOESEN'T MATTER
}
}
Ok, so my problem is that after clicking on my button from the second i got the error mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line I've commented above. I know that after clicking the value of _SESSION var is lost. My question is how to store this thing. I've tried session management but nothing.
I forgot to add, that I'm opening a session in included file.
Use Sessions, and make sure you have a
session_start();
on each page.

If content exists in database, provide form to update it - else provide form to add new row

It's all going wrong. I need to output a form onto my website that will do 1 of 2 things:
If the user already has content in the database, provide a form that posts to self to update the existing content.
If the user does not have content in the database, provide a form to let the user add information to the database.
The forms should submit to themselves to keep coding tidy. I'm getting into a right mess. I'll show what I have so far, but I'm getting in a muddle.
//look in db to see if content exists, if it does set variable
$result = mysql_query(
"SELECT * from tbl_profiles
WHERE user_id = $who
");
while($row = mysql_fetch_array($result))
{
$profileText = $row['text'];
}
// Check if user has content in db
$result = mysql_query(
"SELECT * FROM tbl_profiles WHERE user_id='$who'");
if(mysql_fetch_array($result) !== false){
echo
'<form action="../edit/indexUpdate.php" method="post" name="edit">
Comments:<br />
<textarea name="updatedText" id="comments">' .
$profileText .'
</textarea><br />
<input type="submit" value="Submit" />
</form>'
;}
else{
$profileText = $row['text'];
echo
"<form action='../edit/index.php' method='post' name='add'>
Comments:<br />
<textarea name='comments' id='comments'>" .
$profileText
."</textarea><br />
<input type='submit' value='Submit' />
</form>"
;}?>
You've pretty much got the functionality there, just needs tidying up.
Try something like this:
<?php
//look in db to see if content exists, if it does set variable
$profileText="";
if($result = mysql_query("SELECT * from tbl_profiles WHERE user_id = $who")) {
while($row = mysql_fetch_array($result))
{
$profileText .= $row['text'];
}
?>
<form action="../edit/indexUpdate.php" method="post" name="edit">
Comments:<br />
<textarea name="updatedText" id="comments">
<?php echo $profileText; ?>
</textarea><br />
<input type="submit" value="Submit" />
</form>
<?php
} else {
?>
<form action='../edit/index.php' method='post' name='add'>
Comments:<br />
<textarea name='comments' id='comments'>
<?php echo $profileText; ?>
</textarea><br />
<input type='submit' value='Submit' />
</form>
<?php
}
?>
The basic idea is to add a record if new and update if not. What you can do is use an id to represent the record or -1 if it's a new entry
Something along the lines of:
//Defaults
$recordid=-1;
$name='';
$comments='';
//look in db to see if content exists, if it does set variable
$result = mysql_query(
"SELECT * from tbl_profiles
WHERE user_id = $who
");
// Check if user has content in db
$result = mysql_query(
"SELECT * FROM tbl_profiles WHERE user_id='$who'");
if(mysql_fetch_array($result) !== false){
//Yes. Get the id
$recordid = $result->id;
//Get the values
$name= $result->name;
$comments= $result->name;
}
<form action="../edit/index.php" method="post" name="formdata">
<input type="hidden" name="recordid" value="<? echo htmlspecialchars($recordid) ?>">
<input type="hidden" name="name" value="<? echo htmlspecialchars($name) ?>">
<textarea name="comments" id="comments"><? echo htmlspecialchars($comments) ?></textarea>
<input type="submit" value="submit"/>
</form>
This way a new form will have a -1 but an existing will have an id.
As an additional point it is very important to sanitize your inputs for SQL and what you output in HTML to stop SQL Injections. For your reference on this:
SQL
Little Bobby Tables
Cross Site Scripting

How to get text box value to variable in php and post it with button

i have a code
<form action="index.php" method="post">
<input type="text" name="jam" />
<input type="submit" name="submit" value="Translate" />
</form>
<?php
$conn = mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_set_charset('utf8',$conn);
mysql_select_db ("movedb");
$jam = $_POST['jam'];
$sql = mysql_query("select * from WORD where ENGLISH like '$jam%' Limit 15");
while ($row = mysql_fetch_array($sql)){
echo ' '.$row['ENGLISH'];
echo ' - '.$row['SINHALA'];
echo '<br/>';
}
?>
i want to post that text value like index.php?=text=(text box value here)
and i want to get that submited to $jam
Form method should be get not post.
Also, remove the > from your action. Small typo.
Try:
<form action="index.php" method="get">
<input type="text" name="jam" />
<input type="submit" name="submit" value="Translate" />
</form>
Then you can do: $jam = $_GET['jam'];

Categories