connecting my html to mysql online using php - php

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?

Related

why is not saving to SQL data base from html form? it does not show any error messages

I tried many ways if I insert data to my data base without the
from method it works but with the form and it's method is not working and it doesn't show any error. I don't see any message seems like the code doesn't exist. the connection is good and the name of the table is fine. I am trying to save some data before to implement more code like validation password check and more but if it doesn't save the data how I am going to implement validations. I used the numbers of my serve because it could not be connect with localhost name.
<?php
session_start();
$_SESSION['message'] = "";
$host = "localhost:3308";
$user = "root";
$password = "";
$database = "accounts";
$connect = mysqli_connect($host, $user,
$password,$database); //open the connection.
if(!$connect){
die("cannot connect to database
field:".mysqli_connect_error());
}
else
{
`enter code here`echo "Database is connected". "
<br/>";
if(isset($_POST['create'])){
session_start();
$userName = $_POST['username'];
`enter code here`$email = $_POST['email'];
$pasword = $_POST['pasword'];
$avatar= $_POST['avatar'];
$sql = "INSERT INTO new_table (
username,email,password,avatar)
VALUES (
'$userName','$email','$pasword','$avatar')";
if(mysqli_query($connect, $sql)) {
$_SESSION['message']= "creted";
}
else{
$_SESSION['message']= "not created";
}
}
else {
$_SESSION['message']= "Enter valid data";
}
//my html form
<form action="conectingDB.php" method = "post">
<form action="conectingDB.php" method = "post">
<header>
<h3></h3>
</header>
<br /><br />
<div><?php $_SESSION['message'] ?></div>
<input type="text" name="username" placeholder=
"username" required /><br /><br />
<input type="email" name="email" placeholder = "email"
required /> <br /><br />
<input type="password" name="password"
placeholder="pasword" required /> <br /><br />
<div class = "avatar"><label>Select your avatar:
<input type= "text" name="avatar" ></label></div>
<input type="button" value="create account"
name="create" />
I suggest one more files in your directory with the name "conectingDB.php" and in this file insert the insert command with the connection to the database.
replace the
<input type = "button" value = "create account" name = "create" />
for this
<button type = "submit" value = "create account" name = "create"></button>

registration form can't connect in phpmyadmin db

I have a registration form and I want it to connect to my database. I used phpmyadmin as my database. The code that I used to connect the two is below:
db.php
<?php
$hostname = "127.0.0.1";
$user = 'root';
$password = '';
$db = 'dbTest';
//connection to db
$conn = mysqli_connect("$hostname", "$user", "$password", "$db")or die(mysqli_error());
mysqli_select_db($conn, "peanat")or die(mysqli_error());
$username = $_POST['username'];
$password = $_POST['password'];
$username = strtolower(trim($_POST["username"]));
$username = filter_var($username, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
$checkUsername = mysqli_query($conn, "Select username * FROM users WHERE Username='$username'");
$numrows = mysqli_num_rows($checkUsername);
if($numrows!==1) {
echo "Username not available";
}else{
$sql = "INSERT INTO `users` (`Username`, `Password`) VALUES ('$username', '$password')";
if(!mysqli_query($conn, $sql)) {
die(mysqli_error());
} else {
echo "1 record added";
}
}
?>
this is my reg form
<form id="register" action="db.php" method="post" >
<div class="col-4">
<label>
Username
<input placeholder="" id="username" name="username">
</label>
</div>
<div class="col-4">
<label>Password
<input type="password" placeholder="" id="password" name="password">
</label>
</div>
<div class="col-4">
<label>Confirm Password
<input type="password" placeholder="" id="password2" name="password2">
</label>
</div>
<div class="col-submit">
<input type="submit" class="submitbtn" name="register" value="Register">
</div>
</form>
now the problem I'm encountering is, every time I click the register button, it just goes to the designated page and it shows the code of that page. where do you think is my error...
Place the two files (db.php & reg.HTML) into your local web server folder. (eg. C:/xampp/htdocs/form/
Open any internet browser and type in the path to your HTML file. Eg. Local host/form/reg.html and hit enter.
You should get HTML form displayed on the browser and you're done.

Trying to update MYSQL table in PHP

I've been trying to update a MYSQL table in PHP but it doesn't seem to be working. I tweak the code and sometimes it says it has updated, when it hasn't and other times it says it didn't work. If anybody could have a look at my code and tell me if they can see anything wrong that would be much appreciated.
Form:
<form method="post" action="update.php" name="update" id="update">
<input type="text" name="username" placeholder="Username" id="regUsername" value="<?php echo $row['username'] ?>" /><br><br>
<input type="password" name="password" placeholder="Password" id="regPassword" value="<?php echo $row['password'] ?>" /><br><br>
<input type="email" name="email" placeholder="Email Address" id="regEmail" value="<?php echo $row['email'] ?>" /><br><br>
<p id="FillInFields"></p>
<input type="submit" value="submit"/><br>
</form>
Update.php
<?php
$linkme = mysql_connect("*******","******","******");
if (!$linkme)
die ("Could not connect to database");
mysql_select_db("*******", $linkme);
$username = mysql_real_escape_string($_REQUEST["username"]);
$password = mysql_real_escape_string($_POST["password"]);
$email = mysql_real_escape_string($_POST["email"]);
$edit_id = $_POST['edit_id'];
$query = mysql_query(
"UPDATE user
SET username = '$username' ,
password = '$password' ,
email = '$email'
WHERE user_id = '$edit_id'");
mysql_query ($query)
or die ("Sorry but your details were not uploaded.");
echo ("Your details didn't update");
mysql_close($linkme);
?>
edit_id is the session id that is on the form page: and the session has been started inside all pages using sessions.
$edit_id = $_SESSION['edit_id'];
Thank you
Comment to answer to close the question since it did work for the OP.
Try something like:
<input type="hidden" name="edit_id" value="<?php echo $row['id_row'] ?>">
['id_row'] being an example of the said row, since you're iterating over some type of fetching of rows from DB.
Since you're using sessions, you'll need to assign that POST array to a sessions array after.
Sidenote about the use of the deprecated MySQL library you are using:
Look into using mysqli with prepared statements, or PDO with prepared statements, they're much safer.
I noticed you may be storing passwords in plain text. If this is the case, it is highly discouraged.
I recommend you use CRYPT_BLOWFISH or PHP 5.5's password_hash() function.
For PHP < 5.5 use the password_hash() compatibility pack.
is your variable $edit_id missing in the form file?
<form method="post" action="update.php" name="update" id="update">
<input type="text" name="username" placeholder="Username" id="regUsername" value="<?php echo $row['username'] ?>" /><br><br>
<input type="password" name="password" placeholder="Password" id="regPassword" value="<?php echo $row['password'] ?>" /><br><br>
<input type="email" name="email" placeholder="Email Address" id="regEmail" value="<?php echo $row['email'] ?>" /><br><br>
You must to set an input (usualy of type hidden) to put the key id. In your case, the $edit_id
There are couple of things I've noticed.
To debug your sql query, seperate your sql query into a var
echo $query = "UPDATE user SET username = '$username', password = '$password', email = '$email' WHERE user_id = '$edit_id'";
$query = mysql_query($query); //echo the $query in update.php and
//run the actual query in a sql dialog
//if that doesn't work, then there's
//something definitely wrong with your
//query.
mysql_query ($query)
or die ("Sorry but your details were not uploaded.");
echo ("Your details didn't update"); //you have an echo statement in the
//middle of the script in an area
//without in a conditional loop, it
//will echo this error message even
//if the query committed
//successfully
You're also missing $edit_id. You can have a hidden field in your form as
<input type="hidden" name="edit_id" value="edit_id_val">
You also don't need to put single ticks in your query around the edit_id field value because its an integer.
<?php
$linkme = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die('could not connect because of '.mysqli_error())
$username = mysqli_real_escape_string($linkme, $_REQUEST["username"]);
$password = mysqli_real_escape_string($linkme, $_POST["password"]);
$email = mysqli_real_escape_string($linkme, $_POST["email"]);
$edit_id = mysqli_real_escape_string($linkme, $_POST['edit_id']);
$q = "UPDATE user SET
username = '$username',
password = SHA1('$password'),
email = '$email'
WHERE user_id = '$edit_id'";
$r = mysqli_query($linkme, $q);
if($r){
echo 'success';
}else{
echo '<p>'.$q.'</p>';
echo '<p>'.mysqli_error($linkme).'</p>';
}
?>
you should encrypt the password to using SHA1 or MD5
also you are missing the edit_id

Connecting form to SQL Database

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

How to print the mysql result using form form which is readonly

the output didt display on the readonly form
How to print the sql result to the form which is readonly
Provide html code and action.php code. I have try to use &result on readonly form value
<body>
<form action = "action.php" method="post" >
input: <input type="text" name="v_id" />
<input type="submit" />
</body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('transport_fyp207');
$id = $_POST['v_id'];
$sql = "SELECT v_price " .
"FROM vehicle " .
"WHERE v_id = $id";
$result = mysql_query($sql);
?>
<body>
<label for="textfield">output:</label>
<input name="textfield"
type="text"
id="textfield"
readonly="readonly"
value = "<?php $result ?>">
</body>
you forgot the "echo" and btw you could have the same result with less typing:
<label>
output:
<input type="text" readonly value="<?php echo $result ?>">
</label>
EDIT: as fred -ii- pointed out, this will only work if $result was set. If you want to display something in case of request failure, simply set $result to a default value, something like:
<?php echo $result ? $result : "undefined"; ?>
And also your sql query is incomplete. Read the PHP manual and see how it's done.

Categories