I am able to connect to mysql database however I can not display data from my form to my database. I am not sure why this is happening but I have been able to retrieve data from my database I just can not enter information into it. For now I am just trying to enter First_Name. I also get no errors when entering in data to the form. Any help would be greatly appreciated!!
<p>
<form name="input1" action="http://seanfagan.webuda.com/Final/club.php" method="post">
First_Name:<input type="text" name="First_Name"><br>
Last_Name: <input type="text" name="Last_Name"><br>
Club_Name: <input type="text" name="Club_Name"><br>
Email: <input type="text" name="Email"><br>
Club_Type: <input type="text" name="Club_Type"><br>
Members: <input type="text" name="Members"><br>
<input type="submit" value="Send"><br>
</form>
<?php
$mysql_host = "mysql14.000webhost.com";
$mysql_database = "a9576602_Final";
$mysql_user = "a9576602_Final";
$mysql_password = "*****![enter image description here][1]";
$mysql_error = "Could not connect to database!";
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die ("$mysql_error");
$select_db= mysql_select_db('a9576602_Final') or die ("Couldn't select database!");
$value = $_Post['input1'];
$sql = "INSERT INTO Club (First_Name) VALUES ('First_Name')";
if (!mysql_query($sql)) {
die('Errorss: ' . mysql_error());
}
mysql_close();
?>
</p>
Assuming your connection with the database is okay. You can put a <name> attribute for your submit button to serve as a reference for sending your form via $_POST method:
<input type = "submit" name = "input1" value = "Submit">
Then using this, you can trigger your insert statement:
<?php
if(isset($_POST['input1']))
{
// make sure you also declare the variables in your form such as the first name, etc.
$firstName = $_POST['First_Name'];
mysql_query("INSERT INTO Club (First_Name) VALUES ('$firstName')");
}
?>
Hope this helps you :D
Related
I am new to PHP.
I am trying to add multiple images with text to a database and I'm stuck. If I select one, it gets stored. If I select multiple, it's left as blank in the database.
I need help to resolve this. Plus, I am confused: is one table for images in db ok? I mean, is there a special datatype or way to have multiple images on the same entry in a database?
Here's my code:
<html>
<body>
<form class="container" enctype ='multipart/form-data' action="add.php" method="post">
<label><b> Name: </b></label><br><br>
<input type="text" name="name"><br><br>
<label><b> Type: </b></label><br><br>
<input type="text" name="type"><br><br>
<label><b> Detail: </b></label><br><br>
<input type="text" name="detail"><br><br>
<label><b> Area: </b></label><br><br>
<input type="text" name="area"><br><br>
Select image to upload:
<input type="file" name="filename" size='10000' multiple>
<input type="submit" value="Upload Image">
</form>
<?php
// error_reporting(E_ALL); ini_set('display_errors', 1);
require_once 'login.php';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password, $db_database);
if (!$db_server) die("unable to connect to mysqli:" . mysqli_error());
mysqli_select_db($db_server, "dbase1") or die("db not selected" . mysqli_error());
$name = $_POST["name"];
$type = $_POST["type"];
$detail = $_POST["detail"];
$area = $_POST["area"];
if($_FILES)
{
$namee = $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], $namee);
echo "uploaded image'$namee'<br><img src ='$namee'>";
}
$source= "pictures/".$namee;
$sql = "INSERT INTO adprop (name, type, text,area,filename) VALUES ('$name','$type','$detail','$area','$source')";
$db = $db_server->query($sql);
?>
</body>
<html>
Thanks in advance for your help!
For example if you want save multiple image paths in a field, you can add them to db as below:
/image_folder/image_name1.jpg,/image_folder/image_name2.jpg
And use split and for loop to print them dynamically in user interface.
I am having some trouble trying to delete a member from the database I'm using, I don't think it is getting the Username correctly. Here is the form I am using for HTML
deleteForm.php
<?php
//begin our session
session_start();
?>
<html>
<head>
<title>Welcome</title>
</head>
<form action="deleteUser.php">
<p>
<center><label for="Username">Enter username to delete</center></label>
<center><input type="text" id="Username" name="Username" value="" maxlength="20" /></center>
<center><input type="submit" value="Delete Member"></center>
</p>
</form>
</body>
</html>
And this is the code to handle the deletion itself:
deleteUser.php
<?php
//begin our session
session_start();
//Check if username, password have been sent
if((!filter_input(INPUT_POST, 'Username')))
{
echo 'Please enter a valid username';
}
else
{
//Enter the valid data into the database
$memberUsername = filter_input(INPUT_POST, 'Username', FILTER_SANITIZE_STRING);
echo $memberUsername;
$SQLhostname = "****";
$SQLusername = "****";
$SQLpassword = "****";
$databaseName = "****";
try
{
echo "in the try block";
// Create connection
$conn = mysqli_connect($SQLhostname, $SQLusername, $SQLpassword)
or die("Unable to connect MySQL");
$db_selected = mysqli_select_db($conn, $databaseName)
or die("Could not select database");
$deleteMember = "DELETE FROM customers
WHERE name =
'$memberUsername'";
$result = $conn->query($deleteMember);
if(! $result ){
die('Could not delete member: ' . $conn->error);}
else{
echo "Member deleted <br/>";
}
mysqli_close($conn);
}
catch (Exception $ex)
{
//To be added
}
}
?>
The problem is it always enters the if statement and asks for a valid username which I'm assuming is not being set.
Add method attribute to your form.
<form action="deleteUser.php" method="post">
<!--^^^^^^^^^^-->
<p>
<center><label for="Username">Enter username to delete</center></label>
<center><input type="text" id="Username" name="Username" value="" maxlength="20" /></center>
<center><input type="submit" value="Delete Member"></center>
</p>
Just as a quick FYI:
Whenever a method is omitted in a form, it defaults to GET and you're using INPUT_POST therefore you should either be using INPUT_GET or add a post method, i.e: method="post".
Consult the manual:
http://php.net/manual/en/function.filter-input.php
Plus, and for your added safety, your code is open SQL injection. Do use mysqli with prepared statements, or PDO with prepared statements, they're much safer.
in the form tag add "method" attribute:
<form ... method="POST">
In the PHP script you van find the value of inputs in the variable $_GET:
$_GET[Username'']
Kevin
I need to send my database over to the guy who has the server for a website that I'm managing. Problem is, I'm just now starting to learn PHP and MySQL after only really knowing HTML and CSS. I need to have a form submit data such as FirstName, LastName, Date of Birth, Telephone and etc. through PHP to a MySQL database. Security isn't highly important, as no credit card numbers or anything like that will be going through. But, I'd still like to know how to get the data secure.
Inside of my database, I have a table with all of the entries I need, (FirstName,LastName, DateofBirth, etc.) and I now need to send it to him, so it will be able to store information for the website it self. Here is the problem, I am not exactly sure I am going about the coding portion the correct way.
Here is my HTML:
<html>
<body>
<form action="insert.php" method="post">
<label>First Name: </label><input type="text" name="FirstName">
<label>Last Name:</label> <input type="text" name="LastName"> <br>
<label>Date of Birth:</label> <input type="date" name="DateofBirth">
<label>Telephone:</label> <input type="tel" maxlength="10" name ="Telephone">
<input type="submit">
</form>
</body>
</html>
Here is insert.php
<?php
$con=mysqli_connect("host","username","password","database");
if (mysqli_connect_errno())
{
echo "Error, please try again later.";
}
$sql="INSERT INTO table (FirstName, LastName, DateofBirth, Telephone)
VALUES
('$_POST[FirstName]',
'$_POST[LastName]',
'$_POST[DateofBirth]',
'$_POST[Telephone]')";
if (!mysqli_query($con,$sql))
{
die('Error, please try again later.';
}
echo "Successful";
mysqli_close($con);
?>
Is this correct? Will it work the way that I am needing it to, to where I can pull the information from the table later on? Am I missing any highly crucial components of what I need?
Your code is not correct. $_Post[''] is not directly put in query and this is wrong way to insert data. You should be get data by variables. I have make insert file with database connection file please try this
$host = "hostname";
$user = "username";
$pwd = "password";
$db = "databasename";
$connect = mysql_connect($host, $user, $pwd) or die('Could not connect');
$db = mysql_select_db($db);
$firstname = $_REQUEST['FirstName'];
$lastname = $_REQUEST['LastName'];
$date = $_REQUEST['DateofBirth'];
$telephone = $_REQUEST['Telephone'];
$sql="INSERT INTO `table` (`FirstName`, `LastName`, `DateofBirth`, `Telephone`)VALUES('".$firstname."','".$lastname."','".$date."','".$telephone."')";
if (!mysqli_query($con,$sql))
{
die('Error, please try again later.');
}else{
echo "Successful";
}
Below is my html code...
<form action="http://mycloud.zymichost.com/registerPHP.php" method="post" id="register" data-ajax="false">
<label>Name: <br></label>
<input name="name" type="text" maxlength="50" ><br>
<label>Email: <br></label>
<input name="email" type="text" maxlength="50" ><br>
<label>Password: <br></label>
<input name="password" type="password" maxlength="50" ><br>
<input name="fsubmit" type="button" value="Submit"><br>
</form>
Below is my php code...
<title>registerPHP</title>
<?php
$name = $_POST ['name'];
$mail = $_POST['email'];
$pass = $_POST ['pass'];
//$un = 'abc';
//$pw = 'abc';
$mysql_hostname = "localhost";
$mysql_database = "mycloud_zymichost_register";
$mysql_user = "lorem-ipsum";
$mysql_password = "********";
$conn = mysql_connect($mysql_hostname,$mysql_user,$mysql_password);
mysql_select_db($mysql_database, $conn );
//$query = "SELECT * FROM Login WHERE username = '$un' AND password = '$pw'";
$query = "INSERT INTO Register (name,email,pass) VALUES ('$name','$email','$pass')";
$result = mysql_query($query) or die("Unable to insert user data because : " . mysql_error());
if ($result = mysql_query($query))
echo "Data is inserted";
?>
i cnt send the input data to my database which is mysql online.. may i know why? the sometimes null value is send to the database.. i tried input from my php.. it can.. bt using the post method in html to call the php file to insert.. the value is nt go in.. and everytime i refresh the php page.. null value will be inserted into my db...
Looks like (from a guess) that you're trying to submit the form data to loginPHP but the PHP that saves the data is registerPHP?!
Your formaction has loginphp.php.
Are you sure the php code you posted has the name loginphp.php?
I am trying to submit the page to itself but some reason the following code is not working. Also How can I get the table1 primary key ID back after inserting the data successfully? I have a child table which needs this ID. Thanks for any suggestions.
<?php
include('db_login.php');
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: <br />". mysql_error());
if ($_POST['Submit'])
{
$first = $_POST["first"];
$first = mysql_real_escape_string(get_magic_quotes_gpc() ? stripslashes($first): $first);
$last = $_POST["last"];
$last = mysql_real_escape_string(get_magic_quotes_gpc() ? stripslashes($last): $last);
$insertsql = "INSERT INTO table1(FirstName,LastName) VALUES ('".$first."', '" .$last. "')";
$result1 = mysql_query($insertsql) or die(mysql_error());
}
?>
<form name="hotlineForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
method="post">
<input id="first" type="text">
<input id="last" type="text">
<input type="submit" value="Submit"></form></body>
What part isn't working on the post back? Are you not entering your if statement?
To get the ID of the last insert use the following after your $result1 = mysql_query(...):
$primary_id = mysql_insert_id()
http://php.net/manual/en/function.mysql-insert-id.php
Change your form inputs to include name attributes. Without them, your $_POST will be empty.
<input name='first' id="first" type="text">
<input name='last' id="last" type="text">
<input name='Submit' type="submit" value="Submit">
As mentioned in the comments, get_magic_quotes should not be used. You've correctly called mysql_real_escape_string() on your inputs already.
Following your insert, get the id from mysql_insert_id():
$result1 = mysql_query($insertsql) or die(mysql_error());
$new_id = myqsl_insert_id();
if ($_POST['Submit'])
I don't see a form element with this name.
try:
if (isset($_POST['first']) && isset($_POST['last']))
For getting inserted ID you can use:
mysql_insert_id();
You are missing a closing } here:
if (!$db_select){
die ("Could not select the database: <br />". mysql_error());
} <<---- Close your if statement here.
if ($_POST['Submit'])
Currently the code that does the actual work only gets called if the DB cannot be selected.
Not very useful.
This is why proper indentation is important.
If you are religious about your indentation, you will spot these kind of errors instantly.
Use a name for the input field and check if it was sents not the submit