MY HTML
<div class="normal-text">
<? $check = mysql_query("SELECT * FROM client")or die(mysql_error());
while ($check2 = mysql_fetch_array( $check ))
{
$checkgather = mysql_query("SELECT * FROM gather where client_id = '".$check2['client_id']."' ")or die(mysql_error());
$checkgather2 = mysql_fetch_array( $checkgather );
echo $check2['client_name'].' :
<select name="gather" class=\"form-field\">
<option value="hashtag" '.(($checkgather2['gather_choice']=='hashtag')?'selected="selected"':"").' >hashtag</option>
<option value="latitude" '.(($checkgather2['gather_choice']=='latitude')?'selected="selected"':"").' >latitude/longitude</option>
<option value="followers" '.(($checkgather2['gather_choice']=='followers')?'selected="selected"':"").'>followers</option>
</select>
Start Thread to gather<br>';
}
?>
<br>
<input class="submit-button" type="submit" name="submit" value="Update" />
</div>
I have several rows of results.. each with a dropdown menu of what's in the DB... If I change 1 or multiple values, and I press the UPDATE button... How can I treat the code..
I'm assuming I'll need a foreach(.... )
my current PHP is this:
if (isset($_POST['submit']))
{
$update = mysql_query("UPDATE gather set gather_choice = ' ' where client_id = ' ' ")or die(mysql_error());
}
But I'll probably need a foreach somewhere... Any tips on how to make this work?
thanks
You can do this, assuming you have a unique key on client_id
INSERT INTO gather (gather_choice, client_id)
VALUES (first_choice, first_client_id), (second_choice, second_client_id)
ON DUPLICATE KEY UPDATE client_id = VALUES(client_id)
This will basically try to insert first, but seeing that it already exists, will just update it.
Related
I have a problem with deleting from mySQL table. I'm using drop down list to select which name (id) I need to delete. Please help.
<h1>Delete product</h1>
<form method="post" action = "Delete.php">
<div class="Delete">
<select>
<?php
require('connect.php');
$query = mysql_query("SELECT name FROM `products`");
$id = mysql_query("SELECT id FROM `products`");
while($row=mysql_fetch_array($query)){
echo "<option value='". $row = $_POST['id']."'>".$row['name'].'</option>';
}
?>
</select>
<input type="submit" name="" value="Delete">
</form>
</div>
And this is script. It makes error on line 10 - if(isset($_POST['id'])){
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
require('connect.php');
$id = mysql_query("SELECT id FROM `products`");
if(isset($_POST['id'])){
$id = mysql_real_escape_string($_POST['id']);
$query2 = "DELETE FROM `products` WHERE id = '$id'";
$result=mysql_query($query2);
if($result){
header("Location: tools.php");
exit;
}
else{
echo"ERROR";
}
}
else{
echo"Bad ID";
}
}
?>
Try something like this
//Give select a name so delete.php can hook into it
<select name="product_id">
<?php
require('connect.php');
//Merge your 2 queries into one
$query = mysql_query("SELECT id, name FROM products");
//Fix value fetching in your while loop
while($row=mysql_fetch_array($query)){
echo "<option value='". $row['id']."'>".$row['name'].'</option>';
}
?>
</select>
Then in your submit script
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
require('connect.php');
//Look for select name
if(isset($_POST['product_id'])){
//Get ID from select value
$id = mysql_real_escape_string($_POST['product_id']);
$query = "DELETE FROM products WHERE id = '$id'";
$result = mysql_query($query);
if($result){
header("Location: tools.php");
exit;
}
else{
echo"ERROR";
}
}
else{
echo"Bad ID";
}
}
?>
I havnt tested this but with minor tweaking if any, it should now work for you.
What i have done
Given your a name so it can be picked up by delete.php
Merged your product name, id fetch queries into 1 query
Killed off that $row = $_POST['id'] statement :S
In delete.php checked for the select name (given in bullet 2)
Clean up id depeneding on selected value
Ran delete query
Hope this helps
I am trying to generate a reference id like this CTS-P 0 then CTS-P 1 each time a user submits a form and it gets inserted.
what i have came up with is inserting CTS-P 0 to database as i submit.but the problem is its not incrementing CTS-P 0 to CTS-P 1 after i submit again.
i tried to use mysql_insert_id() here is what i have done so far.this is the smallest thing but i could not solve. please have a look
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("dbname",$con);
$genid="";
if(isset($_POST['submit'])) {
$frstname=$_POST["frstname"];
$genid=mysql_insert_id();
$genid .=count($genid);
//echo $genid;
for($i=0; $i<$genid; $i++) {
$sql = "INSERT INTO tblname (`namecol`,`refidcol`) VALUES ('$frstname','CTS-P $genid[$i]')";
$result = mysql_query($sql);
}}
?>
//here is the form
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="generate" >
First Name<input type="text" name="frstname" />
<input type="submit" name="submit" value="Submit" />
</form>
it is inserting it in my refid column as CTS-P 0 but not incrementing from next time i submit.i know its very noobish but i am stuck.
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("dbname",$con);
if(isset($_POST['submit'])) {
$frstname=$_POST["frstname"];
$sql = "SELECT * FROM tblname";
$genid = mysql_query($sql, $con);
$genid = mysql_num_rows($genid);
//Since you're using "0" as your first number, I decided to comment this out, if not, uncomment it
//$genid++;
$sql = "INSERT INTO tblname (`namecol`,`refidcol`) VALUES ('$frstname','CTS-P $genid')";
$result = mysql_query($sql);
}
?>
I can't seem to find a solution to this and i've looked for similar threads too but no luck
Basically here's my code, when you click Update it's meant to display your current name in the form fields then you can overwrite them and submit the changes, however sadly it will not update, it only displays the originally set first name and last name and does not update the database so therefore not displaying the new set names.
<?php
include('../connect_db.php');
$res = mysqli_query($dbconnection, "SELECT * FROM users");
$row = mysqli_fetch_array($res);
if(isset($_POST['newFirst']) && isset($_POST['newLast'])){
$newFirst = $_POST['newFirst'];
$newLast = $_POST['newLast'];
$id = $_POST['id'];
$sql = "UPDATE users SET first_name='$newFirst', last_name='$newLast' WHERE id='$id'";
$res = mysqli_query($dbconnection, $sql);
}
?>
<div id="editSection">
<h3>Edit Details</h3>
<form action="edit_profile.php" method="POST">
<input type="hidden" value="<?php echo $row[0];?>" name="id"/>
<h2>First Name</h2>
<input type="text" name="newFirst" value="<?php echo $row[1];?>">
<h2>Last Name</h2>
<input type="text" name="newLast" value="<?php echo $row[2];?>">
<input type="submit" value="Update">
</form>
</div>
Any help would be greatly appreciated :)
Kind Regards
~ Matt
You have to connect to DB before updating.so use
$con=mysqli_connect("localhost","my_user","my_password","my_db");
There are several other errors like you have to make $POST['newFirst'] as $_POST['newFirst'] like this
if(isset($_POST['newFirst']) && isset($_POST['newLast'])){
And change the query to
$sql = "UPDATE users SET first_name='$newFirst',last_name='$newLast' WHERE id= '$id'";
beacuse you have error at end of query id='first_name='$id' which is wrong
I see some error in the query
$sql = "UPDATE users SET first_name='$newFirst',
last_name='$newLast' WHERE id='first_name='$id'";
should be
$sql = "UPDATE users SET first_name='$newFirst',
last_name='$newLast' WHERE id= '$id'";
also
if(isset($POST['newFirst']) && isset($POST['newLast'])){
should be
if(isset($_POST['newFirst']) && isset($_POST['newLast'])){
You are using $POST wrong in your if-condition.
It must be called $_POST[..].
Also you should take a look at your WHERE in your update query.
I think you mean: WHERE id= '$id'
You should get your id from $_POST['id']; which is your row ID i suppose and also the update query must be where id=$id.
$id = $_POST['id'];
$sql = "UPDATE users SET first_name='$newFirst', last_name='$newLast' WHERE id=$id";
Also have you checked in DB after the update? the row[0], row[1], row[2] used will have old set of values used during select before the update happened. can you have the mysqli_fetch_array($res) after the update call?
I have the user select a member ID, but I want it to delete the corresponding event ID (PK) so the row is just deleted. Can you suggest a simple and effective way of doing this? This is the code I am working on FYI.
Front end.
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Select member ID to <b> remove total and comment. </b>: <select name="mid">
<?php
while($row = mysqli_fetch_assoc($result))
echo "<option value='{$row['mid']}'>{$row['mid']} </option>";
?>
</select>
<input type="submit" value=">!DELETE!<" />
</form>
<?php
}
else
{
$mid = $_POST['meid'];
$db1 = new dbme();
$db1->openDB();
$numofrows = $db1->delete_total($meid);//basically kill off the entire row
echo "Success. Number of rows affected:
<strong>{$numofrows}<strong>";
Back end method
function delete_total($mid, $meid) {
$sql = "DELETE FROM memberevent WHERE mid = $mid"; // This is the bit I am head scratching, might there be a way to use mid to identify the corresponding PK (meid) to delete so the entire row is killed?
$result = mysqli_query($this->conn, $sql);
if ($result) {
$numofrows = mysqli_affected_rows($this->conn);
return $numofrows;
}
else
$this->error_msg = "could not connect for some wierd reason";
return false ;
}
P.S I am aware it cannot work in its current form.
Here's the memberevent table structure.
meid (PK auto incr INT)
mid (int)
total (varchar)
comments (varchar)
ename (varchar)
I've built a page for user permissions and rankings just not to deal with phpmyadmin every time i want to change someones rank or whatever. Now, I have a problem with this query, every time I run all of the users get the same rank.
$sql = "SELECT * FROM users ORDER BY rank DESC LIMIT $start_from, 20";
$rs_result = mysql_query($sql) or die(mysql_error);
while ($row = mysql_fetch_assoc($rs_result)) {
echo "
<tr>
<td>".$row['username']."</td>
<td>".$row['rank']."</td>
<td>
<form action='' method='post'>
<select name='rank'>
<option value='member'>Member</option>
<option value='moderator'>Moderator</option>
<option value='supermoderator'>Supermoderator</option>
<option value='administrator'>Administrator</option>
</select>
<input type='submit' name='change' value='Change' />
</form>
</td>
</tr>
";
if (isset($_POST['change'])) {
$sql_rank = "UPDATE users SET rank = '".$_POST['rank']."' WHERE username = '".$row['username']."'";
$res_rank = mysql_query($sql_rank) or die(mysql_error());
}
you have
if (isset($_POST['change'])) {
$sql_rank = "UPDATE users SET rank = '".$_POST['rank']."' WHERE username = '".$row['username']."'";
$res_rank = mysql_query($sql_rank) or die(mysql_error());
}
inside the while loop. make sure you close the loop and you grab the data with $_POST not $row...
if (isset($_POST['change'])) {
$sql_rank = "UPDATE users SET rank = '".$_POST['rank']."' WHERE username = '".mysql_real_escape_string($_POST['username'])."'";
$res_rank = mysql_query($sql_rank) or die(mysql_error());
}
Also please consider trying mysqli or PDO
Yeah, you don't transmit the information which user exactly; you only check for isset($_POST["change"]) which isn't serving any information about which user; it's only indicating that any post request with an <input name="change"... /> has been submitted.
I suggest to add the value in some hidden input field like echo '<input type="hidden" name="username" value="'.$row['username'].'" />';
And then add in your if the condition: $_POST["username"] == $row['username']
Also you should consider to upgrade to mysqli or PDO.