Unable to delete mysql data from php - php

I have successfully connected to database and published mysql data on web and manually added joke on it.Screenshot of what output looks like is provided here.
When i manually enter the corresponding id of joke inside the textbox and press delete button, it deletes the joke corresponding to that it. But when i click on the delete button directly, that joke isn't deleted. How to update value of that textbox ? For the time being, i have made input type of textbox as text so to see whats going inside it. I am using PHP version: 7.1.1 and Apache/2.4.25 (Win32.
<p>Add your own joke</p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke) : ?>
<form action ="?deletejoke" method='post'>
<blockquote>
<p><?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8');?>
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
<input type = 'submit' value = 'Delete'>
</p>
</blockquote>
</form>
Screenshot of Mysql database is provided here.

there is a space between <? and php on the input value it should be <?php;
then it should work.
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">

<input type="text" name="id" value="<?php echo $joke['id'];?> ">
you should remove the spaces (not just in this line)

Change
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
to
<input type = "text" name = "id" value = "<?php echo $joke['id']; ?>">

Related

php set input "name" to php echo variable

I have a form with two radio buttons as "yes" and "No"
I m bringing the "name" of the input from PHP variable I get from the DB.
But however, it echoes in the form but when submitting gives the error.
My HTML code goes as:
<form action="phpfile.php" method="post">
<?php
$sql6 = "SELECT * FROM subadminpriv WHERE subadmin_id = '$sa_id'";
$result6 = $conn->query($sql6);
if ($result6->num_rows > 0) {
while($row6 = $result6->fetch_assoc()) {
$sa_id = $row6["subadmin_id"];
$privilege = $row6["privilege"];
$status = $row6["status"];
?>
<div class="togglebutton m-t-30" >
<label >
<strong>⇒ <?php echo $privilege; ?> </strong> <br>
<input name="<?php echo $privilege; ?>" value="1" type="radio">Yes
<input name="<?php echo $privilege; ?>" value="0" type="radio">No
</label>
</div>
<hr>
<?php } }else { } ?>
<button class="btn button" type="submit">Assign Privileges</button>
</form>
Where the $privilege is "Can See Vendors"
So in here, I'm trying to get the fields "sa_id, privilege, status" from the DB and echo as many radio(yes/no) as the fields in DB.
It works, however, and I echoed the "name" field in the input with PHP variable when I inspect it, it gives the value form the DB, but when I submit and echo it, it gives an error.
The PHP phpfile.php page goes as :
<?php include ('../db.php'); ?>
<?php
$priv1 = $_POST['privilege1'];
echo $priv1;
?>
Here "privilege1" is the actual privilege name from the DB field.
while it gives error as "Notice: Undefined index: privilege1 in D:\xampp\htdocs\blah blah blah/pIpfile.php on line 6"
While I Was expecting value I given to radio button like 1 or 0;
Hope i m Clear with my problem. Any Help is Appreciated...
Don't know how I missed it.
The name in the input field won't accept space, so I changed it to "Can_See_Vendors"
Solved

Populate textbox with table entry plus one?

I am trying to increment project number based on the last entry. The the primary key PROJECTNOID auto-increments but is not the same format as the project number (Ex: PROJECTNOID = 1 and Project Number = 19000). I don't want this to be a dropdown box even though some of my code shows the opposite.
<?php
connect = mysqli_connect("**", "**", "**", "**");
$query4 = "SELECT PROJECTNOID, ProjectNumber FROM tblProjects ORDER BY
PROJECTNOID";
$result4 = mysqli_query($connect,$query4);
$options4 = "";
while($row4 = mysqli_fetch_row($result4);){
$options4 = $options4."<input value=$row4[0]$row4[1]</input>";
}
?>
Here is the html textbox:
<label for="txtfield">Project Number</label>
<!--<input type="text" id="reqtxtfield" name="projectnumber"
value="<?php ?>" readonly/>-->
<?php echo $options4;?>
But it would look like how you had it but instead of '1' inside the
box it would display '19000' and there would be nothing outside of the
box other than the label "Project Number". As far as i'm aware you can
assign a value to the text box, regardless of whatever the input is. I
would like it to display the value from one field name but actually
contain the value from a different field name. Both are in the same
table of course.
OK - gotcha. Unfortunately, you cannot do that. A textbox can only have one value and the user is always free to change that value, even if you make it read-only. You can test that out by using the developer toolbar in your browser. Probably a good time to mention that all user input should be considered dangerous and you should never trust it. Once they have submitted the form you need to verify it.
What I would recommend in your case is to use a hidden <input> which contains the value you actually want to submit; projectnoid. You can then display the Project Number in any manner you choose.
<form>
<h1>Project Number: 19000</h1>
<input type="hidden" name="projectnoid" value="1">
<input type="submit" name="submit">
</form>
To generate this, you would:
<?php
while($row4 = mysqli_fetch_row($result4)){
$projectnoid = $row[0];
$projectNumber = $row[1];
echo '<h1>' . $projectNumber . '</h1>';
echo '<input type="hidden" name="projectnoid" value="'. $projectnoid .'">
}
PHP:
<?php
$query_5 = "SELECT MAX(ProjectNumber) FROM tblProjects;";
$result_5 = mysqli_query($conn, $query_5);
$row_5 = mysqli_fetch_array($result_5);
$nextproject=$row_5['MAX(ProjectNumber)']+1;
?>
HTML:
<html>
<form class="myform" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" method="post">
<label for="txtfield">Project Number</label>
<input type="text" id="reqtxtfield" name="projectnumber" value="<?php echo $nextproject ?>" readonly/><br>
After running successful insert query:
echo "<meta http-equiv='refresh' content='0'>"; //REFRESH PAGE TO UPDATE PROJECT NUMBER

Displaying text after space in PHP

I have my website connected to a database with MySQL and I am echoing out the data but it doesn't display information after a space. E.g. in the database 'Hello there' would just be 'Hello'.
I know this is quite a common question asked but I just couldn't get mine working. An example is this:
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
You're missing quotes around your HTML attribute value. Without it the first word is considered the value and everything else is considered a new HTML attribute.
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
Should be:
County: <input type = "text" name = "county" value = "<?php echo $row["county"]; ?>"> <br><br>
//^ ^

Query execution php script in the same page

I have this form html code in update.php. For updating, it's required to link to another page save_seeker.php with mysql update script for it to be executed. Is there any way to execute the script in same page on submitting form so that after query execution it remains on same page?
<form action= "save_seeker.php" method = "post">
Update details !<br><br>
First Name
<input type = "text" name = "fname" value = "<?php echo $disp['fname'];?>"><br><br>
Last Name
<input type = "text" name = "lname" value = "<?php echo $disp['lname'];?>"><br><br>
Contact number
<input type = "text" name = "contact" value = "<?php echo $disp['contact'];?>"><br><br>
Email-id
<input type = "email" name = "email" value = "<?php echo $disp['email'];?>"><br><br>
Address
<input type = "text" name = "address" value = "<?php echo $disp['address'];?>"><br><br>
Experience
<input type = "number" name = "experience" value = "<?php echo $disp['experience'];?> "><br><br>
Qualification
<input type = "text" name = "qualification" value = "<?php echo $disp['qualification'];?>"><br><br>
<input type = "Submit" value = "Update">
</form>
You could do the processing in the same file by adding this into the form action.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<!-- Input fields in here-->
<input type="submit" name="form_submit" value="Submit">
</form>
Now check for the submit action by checking if it is set in the post variable
<?php
if ( isset( $_POST['form_submit'] ) ) {
// Do processing here.
}
?>
You could use the include('page-with-update.php') , call the update function and use the $_SERVER["PHP_SELF"].
I hope helped!!

Update checkbox data in SQL-database with PHP

As the title reveals I got an issue with how to update a checkbox that already has data in my SQL database.
My code looks like following:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="inputAll" value="checked" <?php echo $hemsida['All']; ?>/>Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
<?php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$_POST[inputALL]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
?>
The echo $hemsida['Namn'],['Comment'], and ['All'] just brings up and shows the old data thats in the database, but I do not understand what to do to update the checkbox. I have looked everywhere but I am stuck. Thank you in advance!
If I understand your question correctly, you are looking for a way to have a checkbox be either checked or not checked depending on database info. If so, I would try something like this. At the top of your code where you get your database info, put
if($conditionForCheck){
$inputAll = ' checked="checked"';
}
Then in your form
<input type="checkbox" name="inputAll"<?php echo $inputAll; ?> />
your question is not clear but i think you have a column in your database named "all" ? and perhaps this column can take only 1 value (true or false) !!
then you can test this value in your form, if the value is true : checkbox will be checked, else : checkbox will not be checked :
<input type="checkbox" name="inputAll" checked="<?php if($hemsida['All'] == true) echo checked; ?>" />Alla
dont use value="", use checked instead, then test value of $hemsida['All'] if it's true echo checked else anything to do
for your php code and server side of your application you can just test if checkbox is checked and then you have choice for what do you want to assign to your column in database, for example if checkbox is checked create a variable (for example $value_of_checkbox) and assign a value ("true" for exampel) to this variable, then include this variable in your sql code for update database column :
if (isset($_POST['inputALL'])) {
$value_of_checkbox = true;
}
else {
$value_of_checkbox = false;
}
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$value_of_checkbox' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php")
note : i change also sql code in this part : ALL='$value_of_checkbox'

Categories