Technically it accepts all the input but there no any error message that there is something wrong with my code. The insert is located at the bottom part of the code which is strangely not working because I tried my insert statement in the database and of course changing the variables with real values and it worked that way.
Tried this insert statement in my workbench and it worked:
INSERT INTO members (organization,login,password,name,title,profil,created,logout_time,timezone) VALUES ('1','Kristen','123','Kristen Crooks','Volunteer','2','2016-10-11 14:09','0','0')
Code:
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><b> Volunteer Management </b></center>
<center>
Add User
Add Skills
Assign User
</center>
</head>
<body>
<?php
$config = parse_ini_file('/config.ini');
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT skillName FROM skills";
$result = $conn->query($sql);
$num=mysqli_num_rows($result);
$conn->close();
?>
<form method="get">
Username: <br>
<input type="text" name="username"> <br>
Password: <br>
<input type="text" name="password"> <br>
Name: <br>
<input type="text" name="name"> <br>
Title: <br>
<input type="text" name="title"> <br> <br>
Permission:
<select id="permission" name="permission">
<option selected disabled>Choose here</option>
<option value="0">Admin</option>
<option value="1">Project Manager</option>
<option value="2">Volunteer</option>
<option value="6">Accounting Officer</option>
</select> <br><br>
Skills: <br>
<?php
$i=0;while ($row = mysqli_fetch_assoc($result)) {
$skillName=$row['skillName'];
?>
<input type="checkbox" name="skills" value="<?php echo $skillName; ?>" ><?php echo $skillName; ?><br>
<?php $i++;} ?>
<br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
$config = parse_ini_file('/config.ini');
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$login = $_GET['username'];
$password = $_GET['password'];
$name = $_GET['name'];
$title = $_GET['title'];
$profil = $_GET['permission'];
$created = date('Y-m-d H:i:s');
$sql = "INSERT INTO members (organization,login,password,name,title,profil,created,logout_time,timezone)
VALUES ('1','$login','$password','$name','$title','$profil','$created','0','0')";
$result = $conn->query($sql) or die('Error, insert query failed');
$conn->close();
}
?>
</body>
Okay I already dissected the code and it appears that the $created = date('Y-m-d H:i:s'); is causing the error. By the time I tried not to include it in the data in the insert statement, it worked! I guess I have to get another way to get the date from the system.
Also I've checked that the reason why there is an error is because the date format is in VARCHAR and not in date and another problem is that it does not fit in the length that set. Although I want to ask why it is not detected by the server that there is an error like that
Check PHP log and mysql log, write what is there.
Put an echo before the INSERT, check if your script comes to that place.
If echo is not working- then the problem is not in INSERT
Related
As the title said, I want to display the specific row of data based on the user selection of the year.
If user selected 2017, I only want to display rows of data that are created in the year of 2017.
I know there are something wrong with my query, I am trying to understand how can I write the query. Can anyone please help me? Thank you in advance!
<form action="Report1.php" method="POST">
Select Year:<select name="sortyear" id="sortyear">
<option>2017</option>
<option>2018</option>
</select>
<?php
$sortyear = $_POST['sortyear'];
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = $sortyear)";
$result = mysqli_query($db,$sql);
?>
</form>
Ignoring security flaws, you could do the following:
<?php $getYears = sqlquery() ?>
<form action="Report1.php" method="POST">
<select name="sortyear" id="sortyear">
<?php for ($i = 0; $i < $getYears.length; $i++): ?>
<option value="<?php echo $getYears[$i];?>"><?php echo $getYears[$i];?></option>
<?php endif; ?>
</select>
You can do
<?php for () {
}?>
but the for: allows you to drop back to html code and get your syntax highlighting ;)
I managed to solve it by adding a button
<input type ="submit" name="search" class="btn btn-sm btn-primary" value="Filter" style="background-color: #474747;padding: 5px 5px !important;margin-bottom:5px;">
and coded the php this way
if(isset($_POST['search']))
{
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$sortyear = $_POST['sortyear'];
$sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = $sortyear)";
$result = mysqli_query($db,$sql);
}
You could try this:
<form action="Report1.php" method="POST">
Select Year:<select name="sortyear" id="sortyear">
<option>2017</option>
<option>2018</option>
</select>
<input type="submit" name="submit" value="Get Annual Data" />
<?php
if(isset($_POST['submit'])){
$sortyear = $_POST['sortyear'];
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = " .$sortyear .")";
$result = mysqli_query($db,$sql);
}
?>
</form>
Note that this is mostly your code, I just modified it accordingly to this artcile. The key point is I added button, which enables user to submit form.
This question already has answers here:
PHP update table in MySQL not working
(2 answers)
Closed 6 years ago.
I have received an ID from a get request and put it into new variable $id. I am trying to include this variable in an SQL query but it doesn't work. It does however work when I hardcode the ID.
This works fine.
$query = "UPDATE products SET p_name = " . "'TEST' WHERE p_id=000007;";
The following code does NOT work. Can anyone explain it?
$query = "UPDATE products SET p_name = " . "'TEST' WHERE p_id=" . $id . ";";
All of my code if anyone can help:
<?php
// Connection file
//require 'db.php';
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check Connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$id = $_GET['id'];
echo $id;
if (isset($_POST["updateSubmit"])) {
$query = "UPDATE products SET p_name = " . "'TEST' WHERE p_id=" . $id . ";";
$result = mysqli_query($conn, $query);
}
?>
<div>
<form id="updateForm" name="updateForm" action="update.php" method="post">
<label>Product name:</label><br>
<input type="text" name="updateProductName"><br>
<label>Product type</label><br>
<select name="updateProductType">
<option value="Jackets/coats">Jackets/coats</option>
<option value="Accessories">Accessories</option>
<option value="Shirts">Shirts</option>
<option value="Jeans">Jeans</option>
<option value="Trousers">Trousers</option>
<option value="Shoes">Shoes</option>
<option value="Suits">Suits</option>
</select>
<p>Product description:</p>
<textarea name="updateProductDesc" rows="10" cols="30"></textarea><br>
<label>Product price:</label><br>
<input type="text" name="updateProductPrice"><br>
<label>Stock level:</label><br>
<input type="text" name="updateProductStock"><br>
<input type="submit" name="updateSubmit" value="Submit">
</form>
</div>
<?php
?>
I have used this link from another page which contains the ID.
<a href="update.php?mode=update&id=<?php echo $productDetails['p_id']; ?>"
title="Update <?php echo $productDetails['p_name']; ?>">Update</a>
You didn't get id because there is no id in your url
<form id="updateForm" name="updateForm" action="update.php?id=<?php echo $id; ?>" method="post">
or pass id as hidden like
<input type="hidden" name="id" id="your_id"/>
Change your query to
$query = "UPDATE products SET p_name = 'TEST' WHERE p_id=". $id;
As the title suggests I am trying to link a populated drop down list to a form on another page.
My dropdown list is currently connected to my database which displays the addressID's of 6 people. So when the user selects for example AddressID 3 it will take them to the next page (customerdetails.php) which will then allow them to update the form which will update the database accordingly.
My current code is as follows
<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";
//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
or die("Could not select my_guitar_shop2");
?>
<p>AddressID:</p> <br>
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);
echo "<select name='addressID' onchange = 'getAddressID(this)'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID'] ."</option>";
}
echo "</select>";
?>
Now on the customerdetails.php page i have the code:
<?php
$adrresIDSelected = $_GET['addressID'];
?>
For the life of me I cannot seem to connect the 2 pages together.
Am i anywhere near the correct path? I would prefer not to use javascript as I have no prior knowledge of it.
Many thanks in advance
UPDATE
customerdetails.php page
<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";
//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
or die("Could not select my_guitar_shop2");
?>
<?php
$addrresIDSelected = $_GET['addressID'];
?>
Contact Form
<form class="form">
<p class="first">
<label for="name">FirstLine</label>
<input type="text" name="firstline" id="first" />
</p>
<p class="second">
<label for="email">SecondLine</label>
<input type="text" name="secondline" id="second" />
</p>
<p class="city">
<label for="web">City</label>
<input type="text" name="city" id="web" />
</p>
<p class="state">
<label for="web">State</label>
<input type="text" name="state" id="web" />
</p>
<p class="zip">
<label for="web">Zip Code</label>
<input type="number" name="zip" id="web" />
</p>
<p class="update">
<input type="button" value="Update" />
</p>
<p class="remove">
<input type="button" value="Remove" />
</p>
</form>
First solution (no Javascript)
For a solution without Javascript, you will need to use the select within a form element and use a submit button too to send the information completed/selected in the form to the desired page:
...
<form action="customerdetails.php" method="get">
<select name="addressID">
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID'] ."</option>";
}
?>
</select>
<input type="submit" value="Take me to the other page">
</form>
...
UPDATE - Second solution (with Javascript)
For using the getAddressID Javascript function to send the ID instead of using a form, you will need to update the function a bit:
<script>
function getAddressID (option) {
var addressID = option.value;
// you do not need the <your_domain> prefix here, as probably both your php scripts are on the same server/domain and same folder
window.location.replace ("customerdetails.php?addressID =" + addressID);
//-----------------------------------------------------^
// Extra space must be removed!
}
</script>
I have a select dropdown list that contains all existing usernames found in my table. I want to delete an entry from my table by selecting the corresponding username. I can't seem to find the error in my php file...I get the "Successfully deleted" message even if the entry is still there. Want to know what's wrong with my php. Thanks.
Here's the concerned portion of the View:
<form action="deleting.php" method="post">
<select id="username">
<option ng-repeat="user in users">
{{user.username}}
</option>
</select>
<input type="submit" value="Delete"/>
</form>
Here's my deleting.php file
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "abc";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$deletecontent= isset( $_POST["username"] ) ? $_POST["username"] : "null" ;
$sql = "DELETE from users where username='$deletecontent'";
if (mysqli_query($conn, $sql)) {
echo "Successfully deleted";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
You can get value in $_POST only when there is name attribute in your input. so try
<select id="username" name="username">
You have to add name attribute to select option. Then only it can be accessed using $_POST.
<select id="username" name = "username">
Set name="username" in <select>
and,
set value attribute in <option>.
<form action="deleting.php" method="post">
<select id="username" name="username">
<option ng-repeat="user in users" value="{{user.username}}">
{{user.username}}
</option>
</select>
<input type="submit" value="Delete"/>
</form>
I'm an high school Italian student, and today my professor assigned me a simple homework:
I have to create a PHP page with a basic form that allows me to introduce a new "student" into a table in MySQL, "name", "surname" and "class".
That's the PHP page:
<html>
<head>
<title> School </title>
</head>
<body>
<?php
$name="";
$surname="";
$conn=mysqli_connect("127.0.0.1","root","");
if(!$conn){ echo 'No connection.'; die;}
if(!mysqli_select_db($conn,"School")) { echo 'Err'; die;}
$result=mysqli_query($conn,"select * from classes");
if(!isset($_REQUEST["submit"])){
echo '<form action="" method="post">
<input type="text" name="name" size="26"> Name <br><br>
<input type="text" name="surname" size="26"> Surname <br><br>
<input type="submit" name="submit" value="Submit">';
if(mysqli_num_rows($result)!=0){
echo '<select name="combobox" id="combobox">
<option value="" selected="selected"> Classes list </option>';
while($combobox=mysqli_fetch_assoc($result)){
$classes=$combobox["classes"];
echo '<option value="'.$combobox["classes"].'">'.$combobox["classes"].'</option>';
}
echo '</select><br><br>';
}
echo '</form>';
}
else{
$name=$_REQUEST["name"];
$surname=$_REQUEST["surname"];
//$query='insert into Students(name,surname,classes) values("'.$name.'","'.$surname.'","'.$classes.'");';
//if(!mysqli_query($conn,$query)) { echo 'Err'; die; }
echo 'Success! <br> Name: '.$name.' <br> Surname: '.$surname.'<br> Class: '.$classes.'<br>';
}
?>
</body>
</html>
I have already created the database with all the tables I need. Now, the thing is, how do I put the "option" that I choose from the <select> into a variable so I can use it for the insert into? Everything I found was just "filling a combobox from mysql".
To get value of select give it name and after get it by it's name with $_GET['name'] or $_POST['name'] if you choose method="post", you will get the selected option value.
html:
<form method="get" action="/script.php">
<select name="name"><option>1</option></select>
<input type="submit" value="send">
</form>
php:
$value=$_GET['name'];
echo $value;
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyDB (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This is what you should follow.
Always give your form elements a name, this is how it gets sent to the backend. With <select>, the value of the selected element will be sent to your code via the name you gave it. Once you do this, you can add a variable for it at the end of your code.