Update on a mysql row not working - php

I'm trying to update a column value in a mysql table, but nothing is happening at all.
It's neither showing the update message nor the error message.
html form:
<html>
<form action='xyz.php' method='post'>
<input type='text' size='25' id='country' name='country'>
<input type='submit' id='submit' value='update'>
</form>
</html>
php code:
<?php
session_start();
$con=mysqli_connect("localhost","root","","server2go");
if(mysqli_connect_errno())
echo "<h2>Failed to connect to MySQL database!".mysqli_connect_errno();
if(isset($_POST['submit'])){
$country=$_POST['country'];
$userid=987654326;
$sql2=mysqli_query($con, "UPDATE user_profile set country='$country' WHERE userid='$userid'");
if(!$sql2){
die(mysqli_error($con));
echo "<h6>Failed to update</h6>";
}
else {
echo "<h5>Successfully updated!</h5>";
}
}
?>
what have i done wrong here?
plz help.

The reason why your code is not executing, is that you've set a conditional statement
if(isset($_POST['submit'])){...}
but did not name your submit button:
<input type='submit' id='submit' value='update'>
rename to:
<input type='submit' id='submit' value='update' name='submit'>
You cannot rely on id alone when it comes to POST variables.
Footnotes:
Your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements.
To help protect yourself from an SQL injection attack while using what you have now till you hopefully move over to prepared statements, use:
$userid = (int)987654326; // assuming it's an integer
$country = stripslashes($_POST['country']);
$country = mysqli_real_escape_string($con,$_POST['country']);

Please change
<input type='submit' id='submit' value='update'>
To:
<input type='submit' id='submit' value='update' name='submit'/>
Because, no name attribute is set and control is not going to this condition.
if(isset($_POST['submit'])){
Hope it works for you.

change your submit input in the form
from this
<input type='submit' id='submit' value='update'>
to this
<input type='submit' id='submit' name='submit' value='update'>
mysqli does not automatically secure your query. bind your value.
always leave the error reporting at the time of production by using this line at the top
error_reporting(E_ALL);

Related

Two Forms, One Button. Not sure of how to make this happen

I have a page that prints out rows of information. In each row is a notes box:
<?php
<td style='font-size:12px;width:300px;'>
{$row['Notes']} <br /><center><br />
<form action=\"http://********/functions/notes.php\" id='formNotesform' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<textarea placeholder=\"Add more notes here...\" name=\"notes\" rows=\"5\" cols=\"30\"'></textarea><br />
<input type='submit' name='formNotes' id='formNotes' value='Add to Notes' />
</form>
</center></td>
?>
Then there's also another button on the page in each row.
<form action=\"http://********/functions/archive.php\" method='post' id='formArchiveform' onclick=\"return checkArchive()\">
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formArchive' id='formArchive' value='Archive' style=\"height:25px; width:80px\"/>
</form>
What I need to happen is that when someone clicks the "Add to Notes" button it does its job but when someone clicks the "archive" button it checks to see if notes is empty and if not then it submits that as well (kind of like a failsafe).
Ideally I'd like to just pick up the Notes data and post it to the archived.php file the form is going to anyways since that would cause minimal disruption to the code base but I can't get it to work.
I understand this isn't really a sensible choice. It just has to be done. Thanks for the help!
If I had reputation I would ask first because what I understood is that you want to now what button has been pressed to then do another things.
If that's it do this:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['formNotes']))
{
//...
}
elseif(isset($_POST['formArchive']))
{
//...
}
}

Need help echoing out a input button using PHP

I am trying to echo out this following input submit button Using php.. .
<input type='submit' value='submit' onClick='parent.location='order_summary.php'>
I wrote the following code before but its not working :(
Can anyone please help me out with this? Thanks!
echo "<input type='submit' value='submit' onClick='parent.location="."'order_summary.php'".">";
You need to learn about escaping quotes so you don't write broken HTML:
echo "<input type='submit' value='submit' onClick='parent.location=\"order_summary.php\"'>";
or alternatively (since valid HTML uses double quotes):
echo '<input type="submit" value="submit" onClick="parent.location = \'order_summary.php\'">';

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Submit buttons not working while using type image

This is a simple demonstration of my code :
if(isset($_POST['login'])) {
do this
}
<form method='POST' action="index.php">
Username : <input type='text' name='username'/>
Password : <input type='password' name='password' />
<input type='image' src='images/login.png' name='login' value='login'/>
</form>
The problem is that , when i use type='image' as a submit button, in Firefox and IE nothing happends. But if i use type='submit' everything works fine..
Well i dont want to display the button as a submit one, but as an image, so what im doing wrong ?
Simply change your if to look for any other field in the POST. Image type button wont send that value.
Change:
if(isset($_POST['login'])) {
To
if(isset($_POST['username'])) {
If you want to check login, you need to check for login_x or login_y values instead of login, which is not created when button is an image.
<?php
if(isset($_POST['login_x'])) {
echo "Here I m";
}
?>
<form method='POST' action="index.php">
Username : <input type='text' name='username'/>
Password : <input type='password' name='password' />
<input type='image' src='images/login.png' name='login' value='login'/>
</form>
Sorry, I explained in a comment the type=image creating _x and _y, but I forgot the params login does not exists in that case

PHP: Not going to the next page

I have a little problem with my code and I don't know what is wrong in my code... So I have a two form where the first load is the "view.php" then the second is "checkbox_building.php".. My problem is when I removed the button of delete in "view.php" the dropdown list can proceed to "checkbox_building.php". but when I placed it back, its not working (its not proceeding to "checkbox_building.php") I'm kinda confuse about this.
Here's my code for "view.php"
<fieldset width= "200px">
<form name='form' method='post' action=''>
Select Network: <select name="netName" onChange="this.form.action='checkbox_building.php'; this.form.submit()">
<option value="" >- Select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
$net = $row1[fldNetname];
}
?>
</select>
<input type='submit' name='submit' value='Delete Building/s' onClick="this.form.action='delete_building.php'; this.form.submit()">
</form>
</fieldset>
thanks.!
just as a guess, just from your code:
correct the flaw, then
change
<input type='submit' name='submit'
to
<input type='button' name='dosubmit'
and
<form name='form' method='post' action=''>
to
<form name='<someUsefullName>' method='post' action=''>
and all references to this as well, since "form" is a reserved word in IE and this.form.action may lead to errors there.
just remove name='submit' and try
<input type='submit' name='submit' value='Delete Building/s' onClick="this.form.action='delete_building.php'; this.form.submit()">
so it will be
<input type='submit' value='Delete Building/s' onClick="this.form.action='delete_building.php'; this.form.submit()">
its because this.form.submit is default submit function, but in your code its an input element :)

Categories