Get data not pulling through - php

This is my HTML form:
<form action='buddy_update.php'>
<input type='text' name='buddy1' required='' placeholder='Player ID / E-mail'>
<input type='hidden' name='id' value=''%".$id."%''>
<input type='submit' value='Request Buddy #1!'>
</form>
This is my PHP on buddy_update
<?php
include 'credentials.php';
$id=$_GET['id'];
$buddy1=$_GET['buddy1'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$id=$_GET['id'];
$buddy1=$_GET['buddy1'];
$sql = "UPDATE buddy SET buddy1_id = '".$buddy1."' WHERE main_player = '".$id."'";
if (mysqli_query($conn, $sql)) {
echo $id;
echo $buddy1;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
$buddy1 comes through absolutely fine but $id doesn't.
For what it's worth I have also changed the value='' to a plain text input on the HTML form and it still doesn't work. The output in the PHP form is still blank.
EDIT
No idea why I've defined variables twice! Took the one out but still same problem

There are few things to consider:
Your html form should be in .php file
Your form needs to have method='get', <form action='buddy_update.php' method='GET'>
<input type='hidden' name='id' value="<?php if(isset($_GET['id'])){echo $_GET['id'];}?>"> in case you are getting this GET variable`

Your code can't access the $id value.
Why don't you try this
<input type='hidden' name='id' value='<?php echo $id ?>'>
assuming you define $id above this statement

You have to ensure that the input (hidden) always has a value.
Else it will throw invalid index error.
$id is not getting any value here:

It seems you have defined form in php.Since $id states that variable is defined in php.
so you can try to replace your code with some thing like below code
echo "<form action='buddy_update.php'>
<input type='text' name='buddy1' required='' placeholder='Player ID / E-mail'>
<input type='hidden' name='id' value='".$id."'>
<input type='submit' value='Request Buddy #1!'>
</form>";

Related

MySQL Update with PHP not working how i wanted it to

Hi this is my first post so please excuse any errors
Background if this helps
i've created several pages for a website
submit form - puts data in to database - all working ok
summary page - pulls through elements of database - all working ok
edit page - this is where my problem is
my problem
when creating a edit page it does not pull through the updated variable from the form and just updates the database with a empty field
so here is the code
edit.php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$var_value = $_GET['id'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<form action="/update.php" method="post">
<label>Property Title
<input type="text" name="title" value="<?php echo $row[title]?>" />
<form method='get' action='update.php'>
<input type='hidden' name='id' value= "<?php echo $row[id]?>" >
<input type='submit' class='button radius' value='update' >
</form>
</label>
so that should display whats currently in the database then when a user changes it they click update and it should up date in the database
here is the update.php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$title =$_POST['title'];
$var_value = $_GET['id'];
$result = $conn->query($sql);
$sql = "UPDATE aparthousesalerent SET title='$title' WHERE id = '$var_value' ";
echo $var_value;
echo $title;
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
i added in the extra line of echo $var_value and echo $title; to check if that worked and it did but it still doesn't pass it through to the database
thanks for looking
<form action="/update.php" method="post">
<label>Property Title
<input type="text" name="title" value="<?php echo $row[title]?>" />
<form method='get' action='update.php'>
<input type='hidden' name='id' value= "<?php echo $row[id]?>" >
<input type='submit' class='button radius' value='update' >
</form>
as you can see there are nested form
So, remove the form and make method post because you are using $_POST in php file.
<form action="/update.php" method="post">
<label>Property Title
<input type="text" name="title" value="<?php echo $row[title]?>" />
<input type='hidden' name='id' value= "<?php echo $row[id]?>" >
<input type='submit' class='button radius' value='update' >
</form>
and use one either GET or POST
$title =$_POST['title'];
$var_value = $_POST['id'];

how to update values into database on submit?

I am tring to do a script for update user info from database. For this I have used textboxex to fetch userinfo from db for any userID entered and to edit the same to update. My code is
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
ENTER USER ID TO UPDATE DATA:<br/>
<input type="text" name="userid" value="<?php if(isset($_POST['userid']))echo $_POST['userid'];?>">
<input type="submit" name="submit" value="Find Details"/>
</form>
<?php
if(isset($_POST["submit"])){
$userID=$_POST['userid'];
if(!$userID){
echo "<script>alert('Please fill all the details.')</script>";
exit(0);
}
global $conn; // Create connection
$conn = mysqli_connect('localhost', 'root','root','test');// Check connection
if (!$conn){
die("Connection failed: " . mysqli_connect_error());
}
$query="SELECT * FROM user WHERE ID='$userID'";
$result=$conn->query($query);
$row=$result->fetch_array();
if($row!=null){
$id=$row['ID'];
$name=$row['name'];
$username=$row['username'];
$type=$row['type'];
//$action=echo "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);";
echo "<div id='item'>";
echo "<form action='' method='post'>";
echo"Existing Data is as:<br><br>";
echo "User ID:<input type='text' name='idval' value='$id' wrap='physical'><br><br>";
echo "Name:<input type='text' name='nameval' value='$name' wrap='physical'><br><br>";
echo "User Name:<input type='text' name='userval' value='$username' wrap='physical'><br><br>";
echo "User Type:<input type='text' name='typeval' value='$type' wrap='physical'><br><br>";
// echo "<input type='submit' name='update' value='UPDATE'>";
echo "</form>";
echo "</div>";
}?>
This is working fine and as expected. Now what I want is that as soon as when I hit that update button values changed/edited/updated values should be get updated and then displayed back to user. I have my update query and it is working fine on DB when tested. How can implement the same in php?
Redirect again to the URL you are using to get reflected changes, OR put your update query before select query so you can get updated value in select query.

adding text from html input as value into mysql with php

I wonder if anyone could help me with my problem? I want to send value (input text) to mysql database but it is always blank text. I am the beginner and I think I've made stupid mistake... Code:
<form name="form" method="get">
<input type="text" name="nick">
<input type="text" name="message" height="300px">
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxxxx";
if (isset($_POST['button1']))
{
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$nickval = $_POST['nick'];
$messageval = $_REQUEST['message'];
$sql = "INSERT INTO Messages (nick, message)
VALUES ('$_GET[nick]', '$_GET[message]')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$result = mysqli_query($conn,"SELECT * FROM Messages");
echo "<table border='1'>
<tr>
<th>Nick</th>
<th>Message</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['nick'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
$conn->close();
}
?>
<form method="POST" action=''>
<input type="submit" name="button1" value="Send">
</form>
This:
<input type="submit" name="button1" value="Send">
needs to go inside your first form, where your other inputs are.
<form name="form" method="post">
<input type="text" name="nick">
<input type="text" name="message" height="300px">
<input type="submit" name="button1" value="Send">
</form>
And also #Joe T's answer. Many problems wrong with this question it seems
Your SQL should look more like this, the other answer is also right (about moving your submit button inside the same form with your inputs)
$nickval = mysqli_real_escape_string ( $conn , $_POST['nick']);
$messageval = mysqli_real_escape_string ( $conn , $_POST['message']);
$sql = "INSERT INTO Messages (nick, message)
VALUES ( '$nickval', '$messageval')";
As the commenter wrote, don't use $_REQUEST and $_GET, only $_POST is used here.
And if you don't escape your inputs, (as i've done here with mysqli_real_escape_string) you are asking for a world of hurt.

disabled="true" appears in text box when php/sql query returns null

I want to disable textbutton so that it is not editable. The data in the textbutton comes from a sql query. Disable works fine when the query returns a value. However, when there is no value, it shows "disabled="true"" instead of just being greyed-out.
Here's my code:
<?php
$con=mysqli_connect("hostname","username","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$IMEI = mysqli_real_escape_string($con, $_POST['IMEI']);
$result = mysqli_query($con,"SELECT * FROM mobile
WHERE IMEI LIKE '$IMEI%'");
while($data = mysqli_fetch_array($result)) {
echo "<input type='text' value=".$data['IMEI']." disabled='true'>";
echo "<input type='text' value=".$data['SecIMEI']." disabled='true'>";
echo "<input type='text' value=".$data['Brand']." disabled='true'>";
echo "<input type='text' value=".$data['Model']." disabled='true'>";
echo "<input type='text' value=".$data['Color']." disabled='true'>";
echo "<input type='text' value=".$data['Remarks']." disabled='true'>";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<fieldset><legend>Search Mobile</legend> <br>
<form method="post">
IMEI: <input type="text" name="IMEI"> </pre><input type="submit" action="searchmob2.php">
</form>
</fieldset>
</body>
</html>
Also, how can I hide the php part when the page is loaded?
You're missing quotes around your value attribute. Change:
echo "<input type='text' value=".$data['IMEI']." disabled='true'>";
to:
echo "<input type='text' value='".$data['IMEI']."' disabled='true'>";
Also, you should loop through the while loop in the body of the page - at the moment it's going to echo it before the HTML header.
To "hide" the php part when the page is loaded, you'd simply check if the page was loaded with a post param - like this:
<?php
if( isset($_POST['IMEI']) ) {
...
}
Also, check your quotes:
...
echo "<input type='text' value='".$data['IMEI']."' disabled='true'>";
...

PHP if(isset($_POST...)) condition FAIL

PHP problem here, I've made a login/logout kind code, with a insert and delete function that stand by login and logout.
So the problem is that after I insert the the text I simply cannot delete it, cause the delete button is like a simple
turn back botton, and doesn't make his work, nothing
in the if(isset($_POST['delete'])) condition seems to work.
May the problem be that I'm using two void action
that refer to the same page? cause the first button
work and the second not.
Anyone can understand why?
<html>
<header></header>
<body>
<!-- START PHP -->
<?php
//If not submit i put the submit form
if(!isset($_POST['send'])){
echo "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}<!-- IF END -->
//If submit was set I insert $text into the db and I render
//the delete button
else {
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
$dato=$_POST['dato'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
echo "Operation complete";
//Now i render the delete submit button...
echo "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
//...and if i push it NOTHING, like it's only
//a return to the first form button
if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
echo "<br>Text'".$text."' deleted";
}
}<!-- ELSE END-->
?><!-- END PHP -->
</body>
</html>
There is a logic problem with your code. When the delete button is clicked, the script runs again. The first condition you have - if(!isset($_POST['send'])) will now pass, since the send button is no longer set, and so it goes into the if statement and never runs your delete code.
Your script also appears to be vulnerable to SQL injection.
Here is the right way to do this, it is a quick tip, you need to work a little more on mysql insert security etc.
<html>
<header>
<body>
<?php
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
if(isset($_POST['send'])){
$text = $_REQUEST['text'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
$answer = "Operation complete";
$form = "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
}
else if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
$answer = "Text'".$text."' deleted";
}
else {
$form = "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}
print "<h1>" . $answer . "</h1>";
print $form;
?>
</body>
</header>
</html>
I think it may also work...
if (!isset($_POST['submit']) || isa($_POST['submit'] != 'login'))

Categories