Sorry if my question seems weird, not sure about how to explain it. Here's an example of what my code is looking for :
// SUBMIT FORM
echo '<form action="" method="POST">
<input type="submit" value="Stuff" name="Things"/>
</form>';
// DO THINGS IN MYSQL WHEN CLICKED ON
if (isset($_POST['Things'])) {
$query= $db->prepare('DELETE item_id FROM users WHERE (player_id=:id)');
$query->bindParam(':id', $data['id'], PDO::PARAM_INT);
$query->execute();
}
Now, Imagine I had an image linked to the item_id in the page, for instance :
foreach ($img in $data['item_id']) { // Show IMG }
(This is an example, I may have made some mistakes while writing this but you need to focus on the idea and not the code)
What I would like to do is to make the image (or some text, or some button, or many elements..) when I press the submit button. Because what I have to do right now is making something like this :
if (isset($_POST['Things'])) {
$query= $db->prepare('DELETE item_id FROM users WHERE (player_id=:id)');
$query->bindParam(':id', $data['id'], PDO::PARAM_INT);
$query->execute();
header("Refresh:0");
exit();
}
And submiting already "refresh" the page, so doing this lead in a weird double-page refresh.
Thanks for the help !
Related
I have a form with a download counter on to track downloads. The functionality of this form and the related PHP all works as expected.
What I would like to do is have some functionality where if the value attribute of a hidden form input element is changed by the user (i.e in the dev tools) the related functionality (in this case the download counter) does not happen / is not submitted.
I have other form instances where this problem has been easy to solve because I've had a $_SESSION started on login that matches the user id, so when logged in, users can only amend their own details.
With the download counter though this is public facing (i.e. the user doesn't have to be logged in to download an image). I thought I could assign a variable that matched the $db_image_id that is fetched from the database (i.e has the same numeric value), and ensure this matches the value in the form hidden input element, but this doesn't seem to work?
Also, if there is a better way to approach this problem please do let me know.
Note: $connection is the database connection and $db_image_id is fetched from the MySQL database with fetch() and a PDO statement and then outputted into the form.
PHP
function downloadCounter() {
global $connection;
if (isset($_POST['download'], $_POST['image-id']) && is_numeric($_POST['image-id']) && $_POST['image-id'] > 0) {
// value from hidden form element
$imageID = $_POST['image-id'];
// ** THIS IS THE BIT I THOUGHT WOULD WORK **
// global $db_image_id;
// CREATE ANOTHER VARIABLE THAT MATCHES '$db_image_id'
// $tallyID = $db_image_id;
// if ($tallyID != $imageID) {return;}
try {
$sql = "UPDATE imageposts SET downloads = downloads +1 WHERE image_id = :image_id";
$stmt = $connection->prepare($sql);
$stmt->execute([
':image_id' => $imageID
]);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
}
HTML
<form method="post">
<!-- when anchor link is clicked some javascript adds a click event to the button -->
<a download href="image.jpg">Download Image</a>
<button style="display:none" type="submit" name="download" title="Download"></button>
<input type="hidden" name="image-id" value="<?php echo $db_image_id; ?>">
</form>
Imagine I have a data in the database named warehouse and a whID for the warehouse number. In my add.php I have a form where when you click the submit button, it will add whID data higher than the whID number in the database without using auto increment. I'ved tried a few code for this but nothing works.
this is my code in add.php:
<form action="sidenav/function.php" method="post">
<button type="submit" name="submit" class="button">Add</button>
</form>
This is my code for the function of form in add.php:
if (!isset($_POST['submit'])) {
$insert = $conn->prepare("INSERT INTO warehouse (whID) VALUES (?)");
$insert->bind_param('i', $whNumber);
if ($insert->execute()) {
header('Location: ../warehouseList.php');
die();
}
Sorry for my bad English, I hope you understand my question. Thank you!
If you don't want to use auto-increment just get the highest id from database and increment it yourself.
Quick and dirty example:
$stmt = $conn->prepare("SELECT MAX(whID) as id FROM warehouse LIMIT 1");
$stmt->execute();
$row = $stmt->fetch();
$id = $row["id"] + 1;
Anyway I recommend you to use auto-increment on the id. That's a scenario for what it's created for.
Edit 3: I figured I should try to word, not only my issue but also my end goal better... so here it goes. I need data to be returned to the user by their input. What they put into the form, will return specific data from the database or nothing at all. The database I'm using is information on field reps. When the user enters into the form, they will be looking for specific information. The number they will be asked for will be the repID number. Now... the problem I am having is taking the number that is put into the form, and calling that specific data from the database. The user will not be able to see other data, not associated with other repID's.
Okay, I'm pretty sure I'm not supposed to just delete my entire initial post, but I'm still searching for an answer. Now, I would assume it should be relatively simple, however it has turned out to more taxing than I had originally thought. Perhaps I am not explaining my needs clear enough, I do tend to have that issue a lot.
Here it goes... How to Run sql Query with button click 2.0:
I have a database written in sql and stored on a server. The table I will be accessing is called dataRep and houses Rep Data. This data is user input via form submission. Upon coming to the website there is an option to "View Rep Information" by submitting the Rep's ID that was given. That Rep ID will be the auto increment repID from the table. Upon clicking the button, it opens a new window that should display the rep's data. It does not, however.
Here is the html the user will see:
<div class="pop_box">
<a class="rms-button" href="#popup1">Enter Rep Number Here</a> </div>
<div id="popup1" class="overlay">
<div class="popup">
<a class="close" href="#">×</a>
<div align="center"><br>
<br>
<p>Enter rep number in box below. Submission will open new window.</p>
<form method="get" action="/data/repPrepare.php" target="_blank" >
<input type="text" id="repSelect" name="repSelect" placeholder="Enter Rep Number Given Here" />
<button type="submit" class="newbutton">VIEW</button>
</form>
</div>
</div>
...changed the php a little, but now I get this error:
"Warning: mysqli_query() expects at least 2 parameters, 1 given on line 21
Unable to prepare statement: Query was empty"
<?php
$host = 'mhhost';
$user = 'myuser';
$pass = 'mypass';
$db = 'mydatabase';
$con = mysqli_connect($host,$user,$pass, $db);
//-------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$query = mysqli_query("SELECT * FROM dataRep WHERE repID = ?"); //query
$stmt = mysqli_prepare($con, $query)
or die("Unable to prepare statement: " . $con->error);
$stmt->bind_param("i", $_GET["repSelect"]);
$stmt->execute();
$array = mysqli_fetch_row($result); //fetch result
//-------------------------------------------------------------------------
// 3) echo result as json
//-------------------------------------------------------------------------
echo json_encode($array);
?>
I would like to apologize in advance if I'm totally messing up the procedure for the forum, its just I am truly stuck and have been dealing with this issue for two weeks. Once again, I would appreciate any assistance that can be provided. I just need the php to pull the data tied the repID that the user puts in the box (repSelect).
try like this. You have to print variables bind in bind_result().So
<?php
$stmt = $mysqli->prepare("SELECT repID, RepName, RepBio, RepCerts FROM dataRep WHERE repID = ?");
$stmt->bind_param('i', $_GET['repSelect']);
$stmt->execute();
$stmt->bind_result($repID, $repName,$repBio,$repCerts);
while($stmt->fetch()){
echo $repName;//now it prints RepName
};
$stmt->close();
?>
This question already has answers here:
How can I make sure that PDO's lastInsertId() is not that of another simultaneous insert?
(2 answers)
Closed 7 years ago.
When you go to my website, i have a homepage with a button: Start enquete.
When you click that button, you will see the enquete.
In the meantime, when you clicked that button. A ID has been inserted in the database.
Index.php:
<form method="POST">
<input type="hidden" name="x">
<input type="submit" value="Go to enquete">
</form>
<?php
if(isset($_POST['x'])){
$test = $database->insert_user_id();
//header('Location: enquete/page1.php');
}
And the function:
function insert_user_id(){
$sql = "INSERT INTO user (user_id) VALUES (DEFAULT)";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $this->pdo->('user_id');
}
the return $this->pdo->('user_id'); is something i'm testing with!
Now my question is. How do i return the last inserted id from the table user.
I need to show it on the next page. (See //header).
I can't do this with a other query like: SELECT MAX ID or GetLastInsertId.
This becuase, when some one else also starts the enquete, he will have an other id all of the sudden.
How do i let the person keep the correct id.
Use PDO::lastInsertId
$this->pdo->lastInsertId();
Try this. Manual PDO::lastInsertId
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
You can easily do this by a Get method.
But you can also do this by slightly adjusting your code.
<form method="POST">
<input type="hidden" name="x">
<input type="submit" value="Go to enquete">
</form>
<?php
if(isset($_POST['x'])){
$id = $_POST['X'];
$test = $database->insert_user_id($id);
//header('Location: enquete/page1.php');
}
and change ur function to
function insert_user_id($id){
$sql = "INSERT INTO user (user_id) VALUES ('".$id. "')";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $this->pdo->('user_id');
}
Edit: i failed.
Check this link out http://php.net/manual/en/function.mysql-insert-id.php
If i understand you correctly you want the id of the row you just inserted into the database, and redirect the user to that one?
Index.php
if(isset($_POST['x'])){
$enqueteId = $database->insert_user_id();
header('Location: enquete/page1.php?id=' . $enqueteId);
}
Function:
function insert_user_id(){
$sql = "INSERT INTO user (user_id) VALUES (DEFAULT)";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $this->pdo->lastInsertId();
}
You can make use of PHP mysqli_insert_id() Function which returns the last inserted id of table
For more info go through the link below
http://www.w3schools.com/php/func_mysqli_insert_id.asp
Well I have a problem with my code:
if ($_POST) {
//send confirmation email (or insert into database, etc...)
if(isset($_POST['del'])) {
$Link = $_POST['del_link'];
$query = "UPDATE comentarios SET del = '1' WHERE id = '".$Link."'";
mysql_query($query) or die ('Error: ' . mysql_error());
//header('Location: http://google.es'); //For debug
}
}
echo '<form name="del" method="post">
<input type="hidden" name="del_link" value="'.$rowComen['id'].'" />
Delete
</form>';
But when I press the link the web refreshes and that's all...
I had tried with: header('Location: http://google.es'); But I don't redirect to google...
And I don't know if the problem is in the post or in the query...
if(isset($_POST['del'])) {
You dont seem to have del form field. so the code inside this if statement is never executed. i think you are trying to check for del_link. so make it as if(isset($_POST['del_link'])) {
Have you checked in your browser if it contains the right value? The form as it is will contain the exact value '.$rowComen['id'].', unless a part of the PHP code is missing and the form is actually inside a string..
[edit]
I see. The form's name is 'del', but that name is never sent. Make the name of your submit button 'del', or add another hidden element. Easier still: Just check for the existence of del_link instead of del:
if(isset($_POST['del_link'])) {
$Link = $_POST['del_link'];