In php:
$query ="SELECT rent FROM mydatabase WHERE rent= '$rent'";
$rs=mysqli_query($connect,$query);
if(!$rs)
{echo "<script type ='text/javascript'>alert('Cannot connect to database')</script>";
}
else
{ if(mysqli_num_rows($rs) != 0)
{die("<script type ='text/javascript'>alert('The selected room are not available')</script>");}
}
$sql = "INSERT INTO mydatabase(title,first_name,last_name,dob,email,id,password,rent) VALUES('$title','$first_name','$last_name','$dob','$email','$id','$password','$rent')";
mysqli_query($connect,$sql);
$_SESSION['first_name'] = $first_name;
$_SESSION['rent'] = $rent;
header("location: database.php");
In html:
<select name = "rent">
<option></option>
<option name= "rent" value="A1">A1</option>>
<option name= "rent" value="A10">A10</option>
<option name= "rent" value="A3">A3</option>
<option name= "rent" value="A4">A4</option>
<option name= "rent" value="A5">A5</option>
<option name= "rent" value="A15">A15</option>
<option name= "rent" value="A20">A20</option>
<option name= "rent" value="A8">A8</option>
<option name= "rent" value="A6">A6</option>
<option name= "rent" value="A12">A12</option>
</select>
Here,I want to set unique for rent value. For example, if customer had selected rent value 'A1' again he cannot select the same value. The problem is I also want to set the empty option for customer which does not want to select any option and store their data in database. But the empty option () cannot be entered two time. So what the thing can I give to overcome this. Ps. I am totally amateur and my English is not so good. Thank You :)
I think the best solution for your problem is you need to create your selection dynamically from your database, I mean get what rent didn't take or has been taken by users from your database.
i.e after user loaded the page that selection fills data from database not put it like static. for this, you can create one more table, or just simply add one more field to determines the rent, I called "IsTaken" it returns 1 for Yes, or 0 for not taken.
after user took then update "IsTaken" from 0 to 1 it means it's taken by the user, or after removed you can update from 1 to 0.
for each new record that you or your users insert, add a query to update that field. also for removing each record.
for the update:
update table t1 set rname = 1 where 'your condition to update only this record'
for the delete:
delete from t1 where 'your condition to delete only this record'
please see my solution and let me know if your problem is solved or not.
the following code is just shown selection query.
See this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackoverflow";
$conn = new mysqli($servername, $username, $password, $dbname);
//SELECT those record that not taken, be default i set that field to 0
$sql = "SELECT rname FROM t1 where istaken = 0";
$result = mysqli_query($conn,$sql);
?>
<form action= "" method= "post">
<select name = "rent">
<?php
if ($result->num_rows > 0) {
//fill options with data returns from our query..
while($row = $result->fetch_assoc()) {
echo "<option name=".$row['rname'].">" . $row['rname'] . "</option>";
}
} else {
//if there is no record...
echo "<option name='None'>None</option>";
}
?>
</select>
</form>
<?php
$conn->close();
?>
.
.
.
I am sorry if I have any grammar or spelling error, hope you understand what I am did here.
Related
My question is about a drop-down list which is dependent on the database values which has been assigned to the user which is logged in. I have used a session variable to identify the user logged in.
I would like one drop-down list to display 3 options if the user logged in has a contact ID of 1 OR 2. If the user logged in has a contact ID of 3, then a drop-down list displays with only 2 values.
My code is below which connects to the database and fetches data from a query. If the query values are not empty the first drop-down displays with 2 values, and if the query values are empty, the second drop-down displays with 3 values.
<?php $connect = mysqli_connect('localhost', 'dm459', 'dm459',
'dm459_kiamycontacts'); //This is my connection to the database
$query = "SELECT * FROM employee WHERE contactID = 3 AND
employeeUsername = '" .$_SESSION['User'] . "' LIMIT 1";
//This is the query to the database
$result1 = mysqli_query($connect, $query); ?>
<select id="dropdown">
<?php $row1 = mysqli_fetch_array($result1);
if(!empty($row1)) { ?>
<option class="select" value="0">Kia Academy</option>
<option class="select" value="1">
Dealer Development</option> <?php }
elseif (empty($row1)) { ?>
<option class="select" value="0">Kia Academy</option>
<option class="select" value="1">
Dealer Development</option>
<option class="select" value="2">Dealership</option>
<?php } ?>
</select>
Using contact ID, you could do this:
if(3 == $row1['contactID']) {
// display 2 options
} elseif(2 == $row1['contactID'] || 1 == $row1['contactID']) {
// display 3 options
}
I am trying to update my sql table based off user input. So the user picks from a select list the column they want to update. Then they enter the data from the row and then what they want to update that with. so say the pick column teamname, the would then enter the team name they want to change via text box and then via another textbox enter the new name. Thats what Im trying to get to happen anyways.
I tried to copy and paste my code from where the user deletes data and modify it. I have tried a couple different things, the first thing I tried gave me this error:
UPDATE teams SET rockets = :value1 WHERE teamname = rockets
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rockets' in 'where clause'
My sql statement looked like this:
$sql = "UPDATE teams SET $selectData = :value1 WHERE $columnSelect = $selectData";
Then, I tried to do something like this:
entire code below, as stated above I have tried a couple different thigns with the $sql variable:
php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$columnSelect = $_POST['selectColumn2'];
$selectData = $_POST['selectData'];
$updateData = $_POST['updateData'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$sql = "UPDATE teams SET :value1 = :value2 WHERE $columnSelect = :value1";
// use exec() because no results are returned
$stmt = $conn->prepare($sql);
$stmt->execute(array(':value1'=>$selectData, ':value2'=>$updateData));
if ($stmt->rowCount() > 0) {
echo 'Updated '.$stmt->rowCount().' rows';
} else {
echo 'No rows updated';
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
html
<form method='post' action='addData.php'>
Select a column name, then enter which data to update.
<br>
<br>
<label for='option2'>
<select name='selectColumn2'>
<option value='teamname' id='team2'>teamname</option>
<option value='city' id='city2'>city</option>
<option value='bestplayer' id='best2'>bestplayer</option>
<option value='yearformed' id='year2'>year</option>
<option value='website' id='website2'>website</option>
</select>
</label>
<label for='option2'>
select data to change: <input type='text' name='selectData'>
enter new data: <input type='text' name='updateData'>
</label>
<br><br>
<input type='submit' value='Submit New Entry'>
</form>
Basically what I what to do is take $columnSelect, find $selectData in that column and replace it with $updateData using the PDO method. What am I doing wrong?
This should work: UPDATE teams SET $columnSelect = :value1 WHERE $columnSelect = $selectData
Change your query like this,
$sql = "UPDATE teams SET `$selectData` = ':value1' WHERE `$columnSelect` = '$selectData'";
Add single quotes for string values while inserting or updating. And addd backtics for the table name or column names.
EDIT
$sql = "UPDATE teams SET `$columnSelect` = ':value1' WHERE `$columnSelect` = '$selectData'";
I have a table that prints out all of the users from my table. Next to each user is a select box and a update button. The problem I am having is that the script only reads from the last select box. How can I make the select box dynamic so the script knows which row the select box is from?
code for the select box:
<select name="level" >
<option value="basic" SELECTED>Basic</option>
<option value="premium">Premium</option>
<option value="platinum">Platinum</option>
</select>
<button type="submit" name="update" value="<?php echo $row['id'] ?>">Update</button>
Code in another script that comes into effect when the submit button is pressed:
if ( isset( $_POST['update'] ) ) {
//Get the ID of the account that will be updated
$userID = $_POST['update'];
//Get the level that the admin wishes to change to
$level= $_POST['level'];
//Connect to the DB
mysql_connect("localhost", "xxxx", "xxxx")or die("Cannot connect to database");
mysql_select_db("xxxx")or die("Database cannot be selected or it doesnt exist");
//Updates the users access level
mysql_query("UPDATE users SET level='$level' WHERE id='$userID' ");
Just looking at the html, you are overwriting your select box values as they all have the same name.
The easiest solution would be to convert the name of your select box in an array:
<select name="level[<?php echo $row['id']; ?>]" >
No you can access them server-side as:
$_POST['level'][$userID]
You also need to switch to PDO / mysqli and prepared statements with bound variables to avoid your sql injection problem.
You can use jquery for it. Try this,
$(document).ready(function(){
var values = [];
$('select[name=level]').each(function() {
values[] = $(this).val();
});
$('button[name=update]').val(values);
});
The reason it is only reading the last row, is that $row['id'] contains only the last row from the iterated resultset.
Use this:
<input type="hidden" name="userid[]" value="#THEUSERID"/>
<select name="level[]" >
<option value="basic" SELECTED>Basic</option>
<option value="premium">Premium</option>
<option value="platinum">Platinum</option>
</select>
if ( isset( $_POST['update'] ) ) {
foreach($_POST['userid'] as $k=>$userID){
//Get the level that the admin wishes to change to
$level= $_POST['level'][$k];
//Connect to the DB
mysql_connect("localhost", "xxxx", "xxxx")or die("Cannot connect to database");
mysql_select_db("xxxx")or die("Database cannot be selected or it doesnt exist");
//Updates the users access level
mysql_query("UPDATE users SET level='$level' WHERE id='$userID' ");
}
}
I have a working mysql query that retrieves data from table1.
Now I will add every month a new table (table2, table3..etc).
Goal 1 : I would like to add a drop down menu and populate it with all tables as user options.
Goal 2 : I would like to make the query connect to the table that the user selects from the drop down menu, retrieves data and then refresh the page or just the table's div to display updated data.
my current mysql/php code :
$query = "SELECT X, Y, Z FROM **table1**";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['X'];
echo $row['Y'];
echo $row['Z'];
}
Instead of "table1" it should be a variable linked to the drop down menu that the user selects.
I feel it should be simple, but I am bit new to this and could not find a similar case.
Thanks a lot gents.
I like the comment above but here is an example not sure if that what you are looking for
<form action="process.php" method='post'>
<select name="tables">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" />
</form>
process.php file
$table=$_POST['tables'];
$query = "SELECT X, Y, Z FROM ".$table;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['X'];
echo $row['Y'];
echo $row['Z'];
}
$result = 'SHOW TABLES [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr];';
while($row = mysql_fetch_array($result))
{
echo $row['Tables_from_db_name'];
}
I have this code to load the month of the birthday that corresponds to the id number:
<?php
$query = "SELECT DISTINCT BIRTHDAY FROM student WHERE IDNO='".$_GET['id']."'";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select title="- Select Month -" name="mm" id="mm" class="" >
<?php
while ($row = mysql_fetch_array($result))
list($year,$month,$day)=explode("-", $row['BIRTHDAY']);
?>
<option value="<?php echo $month;?>"><?php echo $month; ?></option>\n";
And this is the form action:
$birthday = mysql_real_escape_string($_POST['mm']);
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
What should I do, when I click on the update button, it executes, but I see the undefined offset error is stored in the mysql database and not the month.
I'm just a beginner, can you give me some tips on how can I achieve updating the data
In cases like this... you will have to use 3 selects and then join them to update the database... so, in the form you have something like this:
<select name='month'>
<option value='1'>January</option>
<option value='xx'>etc</option>
</select>
<select name='day'>
<option value='1'>1</option>
<option value='xx'>etc</option>
</select>
<select name='year'>
<option value='1980'>1980</option>
<option value='xx'>etc</option>
</select>
Then... the PHP that receives that data should do something like:
$year = mysql_real_escape_string($_REQUEST['year']);
$month = mysql_real_escape_string($_REQUEST['month']);
$day = mysql_real_escape_string($_REQUEST['day']);
$birthday = $year.'-'.$month.'-'.$day;
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
Of course... you have to verify first whether all variables are set or not. You can do so by using the isset method.
Check your update query, It may be wrong in that.
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
See this year and firstname, in this year is assigned to null character for you.
Just assign like this,
$birthday = mysql_real_escape_string($_POST['mm']);