activate more than a user at once - php

I'm working on my project for my school, I made activating a user account work, if the admin want to activate one user at a time,
but whenever the admin wants to update multiple record at once, it won't work
I tried to include the update SQL inside a foreach loop, but it gave me an error: invalid argument for foreach()
pleas guys help me,
if(isset($_POST['activate'] )){
$impid = implode(',', $_POST['SSU']);
// for each .. to update more than one at once.
foreach($impid AS $i){
$sql = "UPDATE Accounts SET Activated = '".$_POST['activate']."' WHERE SSU IN('" . $i . "') ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
}
// to test. if anything got effected..
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<script type='text/javascript'> alert('Successfully Updated ')</script>";
}
else {
echo "<script type='text/javascript'> alert('Failed to Update ')</script>";
} // end the test.
}else{echo "<script type='text/javascript'> alert(' Select Whether to Activare Or Deactive ')</script>";}
}
} // end of first if $_post[update]

thank u guys, I fixed it, i just passed $_POST['SSU'] to my foreach and it worked.

Related

Problems With Double-Entry with update

I'm trying to set up an update query to add the last entry with the new entry but my new entry keeps doubling.
I keep looking for error but everything seems ok in not sure why the value of sum keeps doubling.
$sum ='1';
$sql = "update table set old = old +'$sum' where id='1'";
$query=mysqli_query($con,$sql);
if ($con->query($sql) === TRUE) {
echo '<script type="text/javascript">alert("A ok");</script>';
} else {
echo "Bigo Problem: " . $con->error;
}
Try This
$sum =1;
$sql = "update table set old = old +'$sum' where id=1";
if ($con->query($sql) == TRUE) {
echo '<script type="text/javascript">
alert ("A ok");
</script>';
} else {
echo "Bigo Problem: " . $con->error;
}
This is the reason for doubling sum because query exected twice by your code.
$query=mysqli_query($con,$sql);
if ($con->query($sql) === TRUE)
Remove one line and code will work fine.
issue with below two lines.
$query=mysqli_query($con,$sql);
if ($con->query($sql) === TRUE) {
Above two line make two entry.
The problem is that mysqli_query($con,$sql) executes your query....and then $con->query($sql) also executes your query.
So you are running the same query twice from two different commands.
You can just remove $query=mysqli_query($con,$sql); - it isn't needed.

PHP echo break tag

In my PHP script, there are 3 echo, based on the conditionals
echo "Error"
echo "User already existed"
echo "Success"
One of the echo will be passed to my android apps, if (s.equals("Success")), where s is the echo message.
But the echo I got when the registration success is <br />, according to the logcat. For User already existed have no problem.
Since s is not Success, I can't start another activity which is depended on the string. Can anyone explain how the break tag got echoed? The registration information successfully inserted into database, though.
elseif ($roleID == "C") {
$sql6 = "SELECT runnerID FROM tr_customer WHERE customerID = '$newID'";
$check4 = mysqli_fetch_array(mysqli_query($con,$sql6));
if(!isset($check4)) {
// add into db
$customerID = $roleID . $newID;
$sql7 = "INSERT INTO tr_customer(customerID, name, phoneNo, locationID, roleID, email) VALUES ('$customerID', '$name', '$phoneNo', '$locationID', '$roleID', '$email')";
if(mysqli_query($con,$sql7)) {
echo "Success";
}
} else {
$newID = checkExist();
}
I would examine the code where the variable is defined. If you could post that part of the code as an edit then perhaps someone can review it for you as well. There is just not enough information here to discern where your error is coming from.
EDIT:
Consider changing the way you check for a successful update.
$result = mysqli_query($con,$sql7);
if(mysqli_num_rows($result) == 1){
echo "Success";
}

Delete between with a button and $_GET

I'm trying to setup a button that will delete all the rows we loaded on this run but it's not working.
PHP:
if(isset( $_GET['startID'], $_GET['endID']))
{
$sql_query = "DELETE FROM users WHERE id BETWEEN " .$_GET['startID'] . " AND " . $_GET['endID'] . ";";
mysql_query($sql_query);
header("Location: index.php");
}
Button:
<a onClick="javascript: return confirm('Are you sure?');" href='index.php?startID=<?php echo $firstID; ?>&endID=<?php echo $lastID; ?>'>Delete</a>
Button redirects to: index.php?startID=11111&endID=22222
The button is passing the values but they're not getting deleted from the database. Can anyone point me to the mistake?
For this kind of problem you need to start Debugging yourself. What you really need to do is-
Check the database connection is established or not.
Checkout the Condition is correct or not that is applied, You can use a simple echo for this.
echo the query string and run into the phpMyAdmin and see is there anything miss use or others.
These are the suggestion for all the OP who ask this type of
questions. We are always here to help you where you get stuck.
Try for instead:
if(isset( $_GET['startID'], $_GET['endID']))
{
for($i=$_GET['startID'];$i<=$_GET['endID'];$i++){
$sql_query="DELETE * FROM users WHERE id=" .$i. ";";
mysql_query($sql_query);
}
header("Location: index.php");
}

if else header not redirecting

I'm trying to create a redirection but for some reason it is not working and for the life of me I can't figure out why. The if command works well but not the else.
Any suggestions?
$sql="SELECT * FROM $tbl_name WHERE uid='";
$sql=$sql . $xoopsUser->uid("s") . "' ";
// Get a specific result from the "example" table
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
print "<div class='blockTitle'><strong>DOWNLOADS</strong></div><p><br><p>";
echo "<div align='center'>You currently have no projects pending payment.<p>If you just completed a transaction but missed the link on the PayPal page, click <a href='http://site.co.uk/site/modules/cjaycontent/index.php?id=6'>here</a>.</div>";
}
else{
header('Location: http://site.co.uk/site/myComposerList2.php');
}
You need to use exit, header wont stop the script from executing instantly.
Like this:
header('Location: http://site.co.uk/site/myComposerList2.php');
exit;

How to update only fields in a form that are NOT blank

I'm VERY new to MySQL and PHP and have been teaching myself for sometime. I'm not expecting anyone to write my code for me, but I am looking for some suggestions on how best to proceed with this script.
I have a set of users that can update their "skill level" on a particular set of products. At the moment, I have all that working. However, I don't want the user to have to update every skill level each time they submit.
So, in other words, I want them to be able to leave a field blank, but populate other fields with their skill level, thus only updating the fields they have input.
I'm doing this all on a dev server so here is my code that I'm currently working with.
mysql_connect("127.0.0.1","root","time2start") or die("Connection Failed");
mysql_select_db("joomla_dev_15") or die ("Database Connection Failed");
$user = $_POST['user'];
$USP = $_POST['USP'];
$USPV = $_POST['USPV'];
$VSP = $_POST['VSP'];
echo "$user<br />";
echo "$USP<br />";
echo "$USPV<br />";
echo "$VSP<br />";
$query = "UPDATE `joomla_dev_15`.`enterprise_storage` SET `$user` = '$USP' WHERE `enterprise_storage`.`id` = 1;";
if(mysql_query($query))
{
echo "updated<br />";
}else{
echo "FAILURE";
}
$query = "UPDATE `joomla_dev_15`.`enterprise_storage` SET `$user` = '$USPV' WHERE `enterprise_storage`.`id` =2;";
if(mysql_query($query))
{
echo "updated<br />";
}else{
echo "FAILURE";
}
$query = "UPDATE `joomla_dev_15`.`enterprise_storage` SET `$user` = '$VSP' WHERE `enterprise_storage`.`id` =3;";
if( mysql_query($query) )
{
echo "updated<br />";
}else{
echo "FAILURE";
}
Any help or suggestions would be greatly appreciated!
Maybe I'm not understanding this fully, but checking you variables before the UPDATE statement should be enough.
$user = $_POST['user'];
$USP = $_POST['USP']; // Make sure to escape this
if (!empty(trim($USP))) { // Added a trim so that when space is entered, it will still be considered empty
$query = "UPDATE `joomla_dev_15`.`enterprise_storage` SET `$user` = '$USP' WHERE `enterprise_storage`.`id` = 1;";
if(mysql_query($query))
{
echo "updated<br />";
}else{
echo "FAILURE";
}
}
else {
echo "Empty String. Nothing to Do"
}
I was able to figure it out in a way that was satisfactory for me.
Keep in mind, that this code is not necessarily sanitized or secure from malicious attacks, this is simply a dev server so I could prove the concept, and I WILL come back later and secure it.
Also, I've taught myself, so this is most likely not the most efficient way to do this
In order to get the current default values of the user's skills, I had to run this query
$result = mysql_query("SELECT id , user FROM enterprise_storage WHERE id =1");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row1 = mysql_fetch_array($result);
This allowed me to assign a variable to each row that was returned based on this specific query. So, in this case, the person (named "user" in this example) would return a skill level for Product ID "1"
Once I ran this same query for the few items I wanted to assign variables, I could then place this in my html form as the default value for each text box:
Please enter your skill level on the following products:</br>
USP: <input type="text" name="USP" value=<?php echo $row1[user]?> /><br />
USPV: <input type="text" name="USPV" value=<?php echo $row2[user]?> /><br />
VSP: <input type="text" name="VSP" value=<?php echo $row3[user]?> /><br />
I've omitted the rest of the HTML, because its not relevant right now.
In the end, this solves my problem as it places a default value into the form field, and thus the user doesn't need to update that value if they don't want to.
Thanks for all the suggestions!
Check if the form values are blank and do not post them into the database if they are - also, you really need to look at cleansing user input before putting it into the database. See http://php.net/manual/en/function.mysql-real-escape-string.php for example.

Categories