"adding variable into mysqli" - php

I wrote the code below. When I pass $cat_title in myslqi and i print $query it runs the query but in mysql table cat_title field is empty.
<?php
if(isset($_POST['submit'])){
$cat_title = $_POST['cat_title'];
echo "this is cat_title: ".$cat_title."<br>";
if($cat_title ="" ){
echo "title shouldn't be empty";
}
else{
echo $cat_title."this is cat_tiltel";
$query = "INSERT INTO categories(cat_title) ";
$query .= "VALUE('{$cat_title}') ";
echo $query;
$create_category_query = mysqli_query($connection , $query);
if(!$create_category_query){
die("QUERY FAILD".mysqli_error($connection));
}
header("location:categories.php");
}
}
?>
<form action="" method="post">
<div class="form-group">
<label for="cat-title">category title</label>
<input class="form-control" type="text" name ="cat_title">
</div>
<div class="form-group">
<input class = type="submit" name ="submit" value ="Add category">
</div>
</form>

There is a typo error. Please write "VALUES". You have written just VALUE.
Plese try to use below sintex.
INSERT INTO `table_name`(column_1,column_2,...) VALUES (value_1,value_2,...);

Related

Trying to send info to my database from a select tag

Been trying to get this PHP to send to my database but for some reason it won't work but it isn't giving me any errors either. The code probably isn't the prettiest only been working on PHP for 6 months so any help is much appreciated.
<form method="POST">
<div class="form-group">
<label for="exampleFormControlSelect1">Send Message to: </label>
<select class="form-control" id="slectrecipient" name="recipient">
<?php
include("conn.php");
$info = "SELECT FirstName, SecondName, id FROM PT_accounts WHERE NOT id='$accountid'";
$result4 = $conn->query($info);
if(!$result4){
echo $conn->error;
}
while($row4 = $result4->fetch_assoc()){
$recipientfirst = $row4['FirstName'];
$recipientsecond = $row4['SecondName'];
$recipientid = $row4['id'];
echo "<option value='$recipientid'> $recipientfirst $recipientsecond</option>";
}
if(isset($_POST['messagetext'])){
$currentdate = date("Y-m-d H:i:s");
$messagetext = $_POST['messagetext'];
$recipid = $_POST['recipient'];
echo $currentdate;
echo $messagetext;
echo $recipid;
$messageinsert = "INSERT INTO PT_Messages (SenderID, RecipientID, Date, Message)
VALUES ('$accountid', '$recipid', '$currentdate', '$messagetext') ";
$result5 = $conn->query($messageinsert);
if(!$result5){
echo $conn->error;
}else{
echo "<p> $messageinsert</p>";
echo "<p>Message Sent!</p>";
}
}
?>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Message :</label>
<text class="form-control" id="messagetext" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary btn-sm" value="Send">
</form>
I know there's two $row and $results but I've altered these in my actual to be different so I know that's not the issue. I'm unsure if it's the select tag with the option value that isn't written correctly. Or if I have to somehow set the dropdown menu selection as a PHP variable to then be sent to the database?
Thanks to user3783243
I didn't name the textarea but had set it as an id.
<input type="text" class="form-control" name="messagetext" id="messageid" rows="3">
I had it as id="messagetext"

echo is showing output in page source instead of on page

I made a table in php and wanted to show the Id's in the dropdown select menu by making a separate file for php. So the code in main file is:
<?php include "functions.php";?>
<form action="login_update.php" method="post">
<div class="form-group">
<label for="username">username</label>
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="form-group">
<select name="id" id="">
<?php
showAllData();
echo "<br>"."askfkldfjl;adfafladfdf";
?>
</select>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="update">
</form>
The code of functions.php is :
<?php
function showAllData(){
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
echo "We are connected. a=".$a."<br>";
}else{
die("Database connection failed");
}
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
echo("<br>"." <b><h6>We are successful</h6></b>");
}
else {
die("Query FAILED" . mysqli_error());
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
echo"<option value='$id'>$id</option>";
}
}
?>
The expected output was :
But the output is:
So the top two lines in the above screenshot are not printing.
These lines are shown in the INSPECT ELEMENT in chrome.
I forgot to mention the echo command:
echo "<br>"."askfkldfjl;adfafladfdf";
below show all data is also not working.
You made a mistake.
Actually you wrote a code in selectbox and you dont add option so thats why it is not show in html
So write a code like below code so its show in select box as option.
<div class="form-group">
<select name="id" id="">
<option> <?php
showAllData();
echo "<br>"."askfkldfjl;adfafladfdf";
?></option>
</select>
</div>
And If you want to show option from showAllData(); function, the you have return the html.
For this update your showAllData(); function with below code:
function showAllData(){
$options="";
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
echo "We are connected. a=".$a."<br>";
}else{
die("Database connection failed");
}
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
echo("<br>"." <b><h6>We are successful</h6></b>");
}
else {
die("Query FAILED" . mysqli_error());
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$options.="<option value='$id'>$id</option>";
}
return $options;
}
Move the PHP function showAllData() before the HTML <select> element.
Because the <select> element awaits for an <option> element, but all another text will not be visible on page.
E.g.:
<div class="form-group">
<?php showAllData(); ?>
</div>
<?php
function showAllData(){
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
echo "We are connected. a=".$a."<br>";
}else{
die("Database connection failed");
}
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
echo("<br>"." <b><h6>We are successful</h6></b>");
}
else {
die("Query FAILED" . mysqli_error());
}
echo '<select name="id" id="">';
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
echo"<option value='$id'>$id</option>";
}
echo "</select>";
}
?>
Your code is mixed up.
You can use below code. Create an array which gives you values which you needs to show in select.
<?php
function showAllData(){
$idArr = array('msg'=>'','data'=>'','status'=>0);
$connection = mysqli_connect('localhost','root','****','loginapp');
if($connection){
$idArr['msg'] = "We are connected";
$idArr['status'] = 1;
}else{
$idArr['msg'] = "Database connection failed";
$idArr['status'] = 0;
}
if($idArr['status'] == 1){
$query = "SELECT * FROM users";
$result = mysqli_query($connection,$query);
if($result){
$idArr['msg'] = "We are successful";
$idArr['status'] = 1;
}else {
$idArr['msg'] = "Query FAILED" . mysqli_error();
$idArr['status'] = 0;
}
if($idArr['status'] == 1){
while($row = mysqli_fetch_assoc($result)) {
$idArr['data'][] = $row["section_id"];
}
}
}
return $idArr;
}
$idArr = showAllData();
?>
<?php
if(!empty($idArr['data'])){
echo "We are connected<br>";
echo("<br>"." <b><h6>We are successful</h6></b>");
?>
<form action="login_update.php" method="post">
<div class="form-group">
<label for="username">username</label>
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="form-group">
<select name="id" id="">
<option value="0">--Select--</option>
<?php
foreach ($idArr['data'] as $key => $value) {
echo"<option value='$value'>$value</option>";
}
?>
</select>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="update">
</form>
<?php }else{
echo $idArr['msg'];
}
?>
Why you are putting <br> inside the select tag? select tag only accept the options tag under it, so please remove br tag and all extra strings inside the select tag. and you should echo the message above the select tag if you want to show your users.
Thanks

My PHP Code is Not Updating Values In Database

I have tried to write a code that update category in the database using admin panel but whenever i try to do that it won't work and i don't get any errors to look into it, please help guys; thanks a lot
PHP Code:
<?php
if (isset($_GET['edit'])) {
$edit_id = $_GET['edit'];
$query = "SELECT * FROM categories WHERE category_id = $edit_id ";
$edit_get_result = mysqli_query($connection,$query);
if (!$edit_get_result) {
die("Edit Get Result Query FAILED");
}
while ($category_name_row=mysqli_fetch_assoc($edit_get_result)) {
$category_name = $category_name_row['category_name'];
}
?>
<center>
<form action="category.php" method="POST">
<div class="form-group">
<label for="update_category">Update Category</label>
<input type="text" class="form-control" id="update_category" value="<?php if(isset($category_name)){echo $category_name; } ?>" name="update_category" aria-describedby="emailHelp" placeholder="Enter Category Name">
</div>
<button type="submit" name="update_category_submit" class="btn btn-primary">Update</button>
</form>
</center>
<?php
if (isset($_POST['update_category_submit'])) {
$category_name = $_POST['update_category'];
$query = "UPDATE categories SET category_name = '$category_name' WHERE category_id = $edit_id ";
$final_update_query_result = mysqli_query($connection,$query);
if (!$final_update_query_result) {
die("Final Update Query Result FAILED");
}
}
}
?>
Please check below code. You need to pass edit_id in your form POST. I have put it in a hidden input and set it's value according to the GET parameter from top of your php part.
<?php
if (isset($_GET['edit'])) {
$edit_id = mysqli_real_escape_string($connection,$_GET['edit']);
$query = "SELECT * FROM categories WHERE category_id = '$edit_id' ";
$result = mysqli_query($connection,$query);
if(!$result) {
die("Edit Get Result Query FAILED");
}
while ($row=mysqli_fetch_assoc($result)) {
$category_name = $row['category_name'];
}
?>
<center>
<form action="category.php" method="POST">
<div class="form-group">
<label for="update_category">Update Category</label>
<input type="text" class="form-control" id="update_category" value="<?php if(isset($category_name)){echo $category_name; } ?>" name="update_category" aria-describedby="emailHelp" placeholder="Enter Category Name">
</div>
<input type="hidden" name="edit_id" value="<?php if(isset($edit_id)) echo $edit_id;?>">
<button type="submit" name="update_category_submit" class="btn btn-primary">Update</button>
</form>
</center>
<?php
if (isset($_POST['update_category_submit']) && isset($_POST['edit_id'])) {
$category_name = mysqli_real_escape_string($connection,$_POST['update_category']);
$edit_id = mysqli_real_escape_string($connection,$_POST['edit_id']);
$query = "UPDATE categories SET category_name = '$category_name' WHERE category_id = $edit_id ";
$result = mysqli_query($connection,$query);
if (!$result) {
die("Final Update Query Result FAILED");
}
else echo "Final Update Query Result Success";
}
?>
Hi have noticed that you have used raw inputs. try avoiding it. Also noticed your code had extra curly braces at the end.
Please try using the following code after replacing your end page section php script.
if (isset($_POST['update_category_submit'])) {
$category_name = $_POST['update_category'];
$query = "UPDATE categories SET category_name = '$category_name' WHERE category_id = $edit_id ";
$final_update_query_result = mysqli_query($connection,$query);
if (!$final_update_query_result) {
die("Final Update Query Result FAILED");
}
}
And Change your query variable to the following:
$query = "SELECT * FROM categories WHERE category_id = ".$edit_id;

Append GET parameter to php URL

I created a form that populates a database containing customer data. What i'd like to do is that when isset($_POST["submit"]), a GET parameter is appended to the URL, like &customer=<?php echo $last_id ?> where $last_id = mysqli_insert_id($connection);
Is this possible? Would this be a true GET parameter inserted in the GET array? Is also possible to populate the form with this newly added parameter?
Thank you very much!
This is the code, simplified
<?php
if(isset($_POST["submit"]) {
$nome_paciente = mysql_pre($_POST["nome"]);
$query = "INSERT INTO pacientes (";
$query .= "id, nome";
$query .= ") VALUES (";
$query .= "$id, \"{$nome_paciente}\"
$query .= ")";
$result = mysqli_query($connection, $query);
?>
<form action="cadastro_paciente.php?subject=1&paciente=<?php echo $_POST["id"]; ?>" method="post" class="form-cadastro">
<div class="first-value">
<p class="opensans">Nome: </p>
<input type="text" class="table-nome" name="nome" value="<?php echo $nome ?>">
</div>
<input type="text" class="id" name="id" style="display:none" value="$_GET['id']">
<?php } else { ?>
<form action="cadastro_paciente.php?subject=1&paciente=<?php echo $_POST["id"]; ?>" method="post" class="form-cadastro">
<div class="first-value">
<p class="opensans">Nome: </p>
<input type="text" class="table-nome" name="nome" value="<?php echo $nome ?>">
</div>
<input type="text" class="id" name="id" style="display:none" value="$_GET['id']">
?php } ?>
but i keep getting in my url:
&paciente=<br%20/><b>Notice</b>:%20%20Undefined%20index:%20id%20in%20<b>C:\xampp\htdocs\...
Thanks

Update post from database

I'm trying to get my post to update just in case I make a mistake the first time around posting an article to my website.
Not sure what I'm doing wrong here.
Here is my update code:
<div class="row">
<?php
$post_title = "";
$description = "";
$id = $_GET['id'];
$result = mysql_query("SELECT title, description FROM htp_news WHERE id='$id'");
$post_title = mysql_result($result,0,"title");
$description = mysql_result($result,0,"description");
?>
<div class="row">
<form method="post" action="update-news.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<? echo "$id"; ?>">
<div class="grid_12 botspacer60">
Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>">
<br /><br />
News Details:<br />
<textarea id="tiny_mce" name="ud_description" rows="8"><?php echo "$description"; ?></textarea>
</div>
<div class="grid_12">
<input type="submit" value="Update">
<input type="button" value="Cancel" onclick="window.location = '/admin'">
</div>
</form>
</div>
</div>
And here is my action page:
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/database.php");
$ud_id = $_POST['ud_id'];
$ud_title = $_POST['ud_title'];
$ud_description = $_POST['ud_description'];
// Insert record into database by executing the following query:
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' "."WHERE id='$ud_id'";
mysql_query($query);
echo "The post has been updated.<br />
<a href='edit-delete-news.php'>Update another position.</a><br />";
mysql_close();
?>
I appreciate any guidance on the matter.
Add a space before of WHERE Clause in query.
Use below -
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' WHERE id='$ud_id'";
Try this you need quotes in query
$result = mysql_query("SELECT `title`, `description` FROM `htp_news` WHERE id='$id'");
$query="UPDATE htp_news SET `title`='".$ud_title."', `description`='".$ud_description."' "." WHERE `id`='".$ud_id."'";

Categories