Receiving Error Connecting to mySQL server - php

I am self-learning MySQL and PHP, so please bear with me. I am trying to set up a DB with values being input through a PHP form. I am following along in a book, but cannot find why I am getting the error:
Error Connecting to MySQL server.
I am running a local host, root username, no password, and table called alien_abductions.
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_happened = $_POST['whenithappened'];
$how_long_gone = $_POST['howlongwereyougone'];
$howmany = $_POST['howmany'];
$description = $_POST['description'];
$whattheydid = $_POST['whattheydid'];
$fangspotted = $_POST['fangspotted'];
$email = $_POST['email'];
$dbc = mysqli_connect('localhost', 'root', '', 'up_amd_running')
or die('Error connecting to MySQL server');
$query = "INSERT INTO alien_abductions (first_name, last_name, " .
"when_happened, how_long_gone, howmany, description, " .
"whattheydid, fangspotted, email " .
"VALUES ('$first_name', '$last_name',
'$when_happened','$how_long_gone', '$howmany', '$description',
'$whattheydid', `'$fangspotted', '$email')";
echo $query;
$result = mysqli_query($dbc, $query)
or die('Error querying database');
mysqli_close($dbc);

Related

PHP is not inserting Data

I have run it on the local server and it worked perfectly, but when I uploaded it to my web hosting, it stopped working.
Basically when I submit the form the browser just keeps loading and when I check the database no data was inserted.
I checked my database connection and I was able to connect it but can't get data from it.
This is my php:
<?php
$servername = "localhost";
$username = "itclubac_root";
$password = "*******";
$dbname = "itclubac_itclub";
$tnp = 0;
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$id = $_POST['id'];
$section = $_POST['section'];
$skills = $_POST['skills'];
$interests = $_POST['interests'];
$expectations = $_POST['expectations'];
$tnp = $_POST['tnp'];
$ip = $_SERVER['REMOTE_ADDR'];
if ( $tnp == 0 ) {
header('Location: ../../get_involved.php');
} else {
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$query = mysqli_query($con, "SELECT * FROM member_registration WHERE email = '".$email. "'");
if ( mysqli_num_rows($query) > 0 ) {
header('Location: ../../get_involved.php?status=exist');
} else {
$query = mysqli_query($con, "SELECT * FROM member_registration WHERE college_id = '".$id. "'");
if ( mysqli_num_rows( $query) > 0 ) {
header('Location: ../../get_involved.php?status=exist');
} else {
$sql = "INSERT INTO member_registration (name, email, gender, phone_no, college_id, section, skills, interests, expectations, ip_address) VALUES ('$name', '$email', '$gender', '+880$phone', '$id', '$section', '$skills', '$interests', '$expectations', '$ip')";
if ($con->query($sql) === TRUE) {
header('Location: ../../get_involved.php?status=success');
}
}
}
}
$con->close();
?>
Edit
This is the site: Form Pagehttp://itclub.acc.edu.bd/get_involved.php if you register here, the page will just keep loading. However if you try to access the registration.php directly it sends you to the form page page as I said it to. When I tested it on local, it worked perfectly but after uploading to the host this problem is occurring.
I tried to sort your code and maybe the error is related on the of your query,
$sql = "INSERT INTO member_registration (name, email, gender, phone_no, college_id, section, skills, interests, expectations, ip_address) VALUES ('$name', '$email', '$gender', '+880$phone', '$id', '$section', $'skills', '$interests', '$expectations', '$ip')";
Have you notice this part $'skills' of the line? change your code into,
$sql = "INSERT INTO member_registration (name, email, gender, phone_no, college_id, section, skills, interests, expectations, ip_address) VALUES ('$name', '$email', '$gender', '+880$phone', '$id', '$section', '$skills', '$interests', '$expectations', '$ip')";
maybe it help.

Updating MySQL Error but nothing is wrong in code?

So I get this error:
Problem updating record. MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE KittenID = '2''
But then in my code:
<?php
if(isset($_POST['Modify']))
{
$connection = mysql_connect("Deleted the login info");
// Check connection
if (!$connection)
{
echo "Connection failed: " . mysql_connect_error();
}
else
{
//select a database
$dbName="Katz";
$db_selected = mysql_select_db($dbName, $connection);
//confirm connection to database
if (!$db_selected)
{
die ('Can\'t use $dbName : ' . mysql_error());
}
else
{
$KittenID = $_POST["KittenID"];
$KittenAge = $_POST['KittenAge'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Gender = $_POST['Gender'];
$Personality = $_POST['Personality'];
$Activity = $_POST['Activity'];
$Comments = $_POST['Comments'];
$query = "UPDATE Kittenzz
SET KittenID = '$KittenID',
KittenAge = '$KittenAge',
Name = '$Name',
Email = '$Email',
Gender = '$Gender',
Personality = '$Personality',
Activity = '$Activity',
Comments = '$Comments',
WHERE KittenID = '$KittenID'";
$res = mysql_query($query);
if ($res)
{
echo "<p>Record Updated<p>";
}
else
{
echo "Problem updating record. MySQL Error: " . mysql_error();
}
}
}
mysql_close($connection);
}
?>
It makes no sense, I've read those lines of code for an hour, I cannot see the problem. It should run. Can anyone lend me fresh eyes?
Remove the comma near '$comments'
$query = "UPDATE Kittenzz
SET KittenID = '$KittenID',
KittenAge = '$KittenAge',
Name = '$Name',
Email = '$Email',
Gender = '$Gender',
Personality = '$Personality',
Activity = '$Activity',
Comments = '$Comments'
WHERE KittenID = '$KittenID'";
it may possible that in connection username and password in required.
like this :-
$connection = mysql_connect("localhost","username","password");

Trying to insert multiple form data into multiple tables to the database, no error given but not working

When I run my code, I get no errors and nothings being sent to the database as well and I can't seem to figure out what the problem could be here ?
I am new to this forum and mysql and php as well and Im not really sure if this is the right way of inserting the datas when you have multiple tables to fill in
or it could be something to do with the incorrect html input attributes?
$db = mysql_connect($dbhost, $dbusername, $dbpass);
$db_select = mysql_select_db($dbdatabase, $db);
if (!$db_select) {
die ("Unable to select database: " . mysql_error());
}
$query = "SELECT * FROM members, login, skills, indivoffers";
$result = mysql_query($query);
if (isset($_POST['mrmrs'],$_POST['fname'],$_POST['lname'],$_POST['gender'],$_POST['addr1'],
$_POST['addr2'],$_POST['city'],$_POST['postcode'],$_POST['hometel'],$_POST['mobtel'],
$_POST['email'],$_POST['job'],$_POST['user'],$_POST['pass'],$_POST['skill1'],
$_POST['skill2'],$_POST['skill3'],$_POST['skill4'],$_POST['skill5'],$_POST['skill6'],
$_POST['skill7'],$_POST['skill8'],$_POST['skill9'],$_POST['ortitle'],$_POST['message'],
$_POST['offereq'],$_POST['cost'],$_POST['pay'])){
$title = $_POST['mrmrs'];
$name = $_POST['fname'];
$name2 = $_POST['lname'];
$gender = $_POST['gender'];
$address1 = $_POST['addr1'];
$address2 = $_POST['addr2'];
$city = $_POST['city'];
$pc = $_POST['postcode'];
$telhome = $_POST['hometel'];
$telmob = $_POST['mobtel'];
$email = $_POST['email'];
$job = $_POST['job'];
$username = $_POST['user'];
$password = $_POST['pass'];
$skill1 = $_POST['skill1'];
$skill2 = $_POST['skill2'];
$skill3 = $_POST['skill3'];
$skill4 = $_POST['skill4'];
$skill5 = $_POST['skill5'];
$skill6 = $_POST['skill6'];
$skill7 = $_POST['skill7'];
$skill8 = $_POST['skill8'];
$skill9 = $_POST['skill9'];
$titleor = $_POST['ortitle'];
$mess = $_POST['message'];
$offerequest = $_POST['offereq'];
$cost = $_POST['cost'];
$pay = $_POST['pay'];
$sql = "INSERT INTO members (Mr/Mrs, fname, lname, gender, DOB, addr1, addr2, city, postcode, telnohome, telnomob, email, job)
VALUES ('$title','$name','$name2', '$gender', '$address1', '$address2', '$city', '$pc', '$telhome', '$telmob', '$email', '$job')";
$letsid = mysql_insert_id( $db);
$sql = "INSERT INTO login (letsID,username, password)
VALUES (letsID),'$username','$password')";
$letsid = mysql_insert_id( $db);
$sql = "INSERT INTO skills (letsID, skill1, skill2, skill3, skill4, skill5, skill6, skill7, skill8, skill9)
VALUES (letsID,'$skill1', '$skill2', '$skill3', '$skill4', '$skill5', '$skill6', '$skill7', '$skill8','$skill9')";
$letsid = mysql_insert_id( $db);
$sql = "INSERT INTO indivoffers (letsID, title, message, offer/request, cost, pay)
VALUES (letsID,'$titleor','$mess', '$offerequest', '$cost', '$pay')";
$letsid = mysql_insert_id( $db);
}
?>
You are creating the querys correctly but you aren't actually executing them.
So instead of doing <?php $sql = "INSERT INTO ..."; ?> you can do something like this
<?php $sql = mysql_query("INSERT INTO ..."); ?>
Setting the variable to a mysql query will execute the query so therefore this should run your querys now.

Insert form data in one table and then update an enum value in a different table?

I am using a code to insert a users form data into the database, that bit works fine. However, i also want to run another query and update an enum value 'form2_completed?' from No to Yes. I have added in the update query and now for some reason the script is not working and says 'ERROR'
Can someone please show me where i am going wrong. thanks
<?php
session_start();
$db_hostname = 'localhost';
$db_database = 'hewden1';
$db_username = 'root';
$db_password = '';
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$cname = $_POST['cname'];
$creg = $_POST['creg'];
$address = $_POST['address'];
$post = $_POST['post'];
$contactn = $_POST['contactn'];
$contactt = $_POST['contactt'];
$email = $_POST['email'];
$vat = $_POST['vat'];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";
$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";
$result = mysql_query($sql);
if($result){
$success = "<div class='success'></div>"; // use the $success
//encode the URL parameter as :
$success = urlencode($success);
header("Location: index.php?success=$success");
}else {
echo "ERROR";
}
?>
You are overwriting the variable $sql and not running INSERT. Try:
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";
$result = mysql_query($sql);
$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";
$result = mysql_query($sql);
Please note that the method you have used is deprecated from php 5.5.0. so i suggest you consider mysqli or PDO. examples can be found in below php manual links
http://www.php.net/manual/en/mysqli.query.php
http://www.php.net/manual/en/pdo.query.php

storing ip address in mysql?

i am trying to store a users ip address into a mysql table under VARCHAR (39) but its storing as just this "::1"
i am using this code:
<?php
session_start();
$db_hostname = 'localhost';
$db_database = 'hewden1';
$db_username = 'root';
$db_password = '';
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$cname = $_POST['cname'];
$creg = $_POST['creg'];
$address = $_POST['address'];
$post = $_POST['post'];
$contactn = $_POST['contactn'];
$contactt = $_POST['contactt'];
$email = $_POST['email'];
$vat = $_POST['vat'];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";$result = mysql_query($sql);
if($result){
echo "jobs a gooden";
}else {
echo "ERROR";
}
?>
can someone please show me where i am going wrong thanks
Simple solution, change your ip column to varbinary (16) datatype and then store ips using the following:
$ip = bin2hex(inet_pton($_SERVER['REMOTE_ADDR']));
Note: make sure your php version is up to date as inet_pton() is a new function.

Categories