Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP to MySQL submission

Featured Replies

Hi, This is a first post, i'm a web designer more on the visual front end side of things, my PHP/MySQL knowledge is pretty basic, usually leave that to my web dev colleague but he's out of action for the foreseeable.

 

Here's a my question...

 

I have a form which captures a web address, I would like this web address stored in MySQL, have the following code for PHP...

 

<?php
$con = mysql_connect("localhost","XXXXX","XXXXX"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("websitevoice_", $con); 
$url=mysql_real_escape_string($_POST['url']); 
$ref=mysql_real_escape_string($_POST['ref_id']); 
$sql="INSERT INTO web_addresses (link_url,ref_id) VALUES ('$url','$ref')"; 
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>

 

Here's the SQL dump...

 

CREATE TABLE `web_addresses` (
 `link_id` bigint(20) NOT NULL auto_increment,
 `link_url` varchar(255) NOT NULL default '',
 `ref_id` varchar(20) NOT NULL default '',
 PRIMARY KEY  (`link_id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

-- 
-- Dumping data for table `web_addresses`
-- 

INSERT INTO `web_addresses` (`link_id`, `link_url`, `ref_id`) VALUES (1, 'www.google.com', '123'),
(8, '', ''),
(9, '', ''),
(10, '', ''),
(11, '', ''),
(12, '', '');

 

The form processes, so it's connecting to the db and as you can see from the dump it's creating the new entries against a unique link_id but the values aren't coming through.

 

Probably making some kind of school boy error, any help would be much appreciated.

Thats because you haven't got an action.

 

<?php
$con = mysql_connect("localhost","XXXXX","XXXXX"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("websitevoice_", $con); 
$url=mysql_real_escape_string($_POST['url']); 
$ref=mysql_real_escape_string($_POST['ref_id']); 
$sql="INSERT INTO web_addresses (link_url,ref_id) VALUES ('$url','$ref')"; 
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>

 

This is going to insert a new row everytime the page is loaded.

 

Do this:

 

 

<?php
$url=(isset($_POST['url'])) ? mysql_real_escape_string($_POST['url']) : FALSE; 
$ref=(isset($_POST['ref_id'])) ? mysql_real_escape_string($_POST['ref_id']) : FALSE; 

if ($url && $ref)
{
$con = mysql_connect("localhost","XXXXX","XXXXX"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("websitevoice_", $con); 


$sql="INSERT INTO web_addresses (link_url,ref_id) VALUES ('$url','$ref')"; 
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";



mysql_close($con);
}
?>


Remove the '' around the values, for the sql

 

e.g.

$sql="INSERT INTO web_addresses (link_url,ref_id) VALUES ($url,$ref)"; 

Hey dude here you go this code of mine works fine if you have any probs with it let me know and if you wanna know the changes i made to urs to make it work just ask cheers

 

 


 mysql_connect("localhost","root","") or die ("Ca't connect to database");
		  mysql_select_db(websitevoice_) or die ("Can't select from database");


		$url=mysql_real_escape_string($_POST['url']); 
		 $ref=mysql_real_escape_string($_POST['ref_id']); 

		   if(isset($_POST['submit'])){
		  $sql="INSERT INTO `web_addresses` (`link_id`,`link_url`,`ref_id`) VALUES ('link_id','".$url."','".$ref."')"; 
		  mysql_query($sql) or die (mysql_error());
		 echo "The form data was successfully added to your database.";
		 mysql_close();
		}

Thats because you haven't got an action.

 

<?php
$con = mysql_connect("localhost","XXXXX","XXXXX"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("websitevoice_", $con); 
$url=mysql_real_escape_string($_POST['url']); 
$ref=mysql_real_escape_string($_POST['ref_id']); 
$sql="INSERT INTO web_addresses (link_url,ref_id) VALUES ('$url','$ref')"; 
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>

 

This is going to insert a new row everytime the page is loaded.

 

Do this:

 

 

<?php
$url=(isset($_POST['url'])) ? mysql_real_escape_string($_POST['url']) : FALSE; 
$ref=(isset($_POST['ref_id'])) ? mysql_real_escape_string($_POST['ref_id']) : FALSE; 

if ($url && $ref)
{
$con = mysql_connect("localhost","XXXXX","XXXXX"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("websitevoice_", $con); 


$sql="INSERT INTO web_addresses (link_url,ref_id) VALUES ('$url','$ref')"; 
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";



mysql_close($con);
}
?>


He said hes new at this the way you put it prob is alot harder for him to understand the easyest way from him to do it is just to check if the submit button was clicked and if so insert the data but also that does not cover checking the fields if they were left blank or not which urs does but idk if he wanted that or not

He said hes new at this the way you put it prob is alot harder for him to understand the easyest way from him to do it is just to check if the submit button was clicked and if so insert the data but also that does not cover checking the fields if they were left blank or not which urs does but idk if he wanted that or not

 

 

I'm not sure how it's much harder.. a simple google search could have turned up what i've done in my example. Mine does not check if the submit button is blank - Your example is bad practice.. You're not checking if each value actually exists in the _POST superglobal - your example would throw up an undefined index error, which in turn would poison the error log with totally pointless errors - it's a matter of teaching someone how to code, not stopping at "it just works."

 

He should also implement some further checking (e.g. structure of a url using preg_match etc)

  • Author

Thanks for the replies guys, the form is actually on a different page, this php is for pulling out the variables from the url and adding them to the db i.e. ?url=www.google.co.uk&ref_id=123

 

I've tried both methods mentioned here with no joy. I believe my original code is submitting to the db but no values are being stored for some reason, I'm thinking could I have issue with my sql syntax?

Thanks for the replies guys, the form is actually on a different page, this php is for pulling out the variables from the url and adding them to the db i.e. ?url=www.google.co.uk&ref_id=123

 

I've tried both methods mentioned here with no joy. I believe my original code is submitting to the db but no values are being stored for some reason, I'm thinking could I have issue with my sql syntax?

 

If you can send me your entire php code including the form to this email webtech83@hotmail.com and i'll fix it up for u

I'm not sure how it's much harder.. a simple google search could have turned up what i've done in my example. Mine does not check if the submit button is blank - Your example is bad practice.. You're not checking if each value actually exists in the _POST superglobal - your example would throw up an undefined index error, which in turn would poison the error log with totally pointless errors - it's a matter of teaching someone how to code, not stopping at "it just works."

 

He should also implement some further checking (e.g. structure of a url using preg_match etc)

 

Oh no i agree with u 100% urs is the better way but for the point of just showing him how to insert i used my code

  • Author

If you can send me your entire php code including the form to this email webtech83@hotmail.com and i'll fix it up for u

 

Cheers for that, will do. Thanks to Kieran too.

Cheers for that, will do. Thanks to Kieran too.

Make sure you don't get charged ;)

Ok heres the code for you works fine :clapping: i also sent it to your email hope this helps

 

 

 


<?php 

$CONFIG['DB_CONFIG'] = array(
'server'   => 'localhost',
'user'     => 'root',
'password' => '',
'database' => 'websitevoice_'
);


 $con = mysql_connect($CONFIG['DB_CONFIG']['server'],$CONFIG['DB_CONFIG']['user'],
$CONFIG['DB_CONFIG']['password']);
if(!$con){
die("Can't connect to database");
}
mysql_select_db($CONFIG['DB_CONFIG']['database'],$con);
function protect($string){
$string = mysql_real_escape_string($string);

return $string;

}



$url = protect($_POST['url']);
$ref = protect($_POST['ref_id']);


if($url && $ref){

$sql = "INSERT INTO web_addresses (`link_url`,`ref_id`) VALUES('".$url."','".$ref."')"; 

if(!mysql_query($sql,$con)){

die(mysql_error());
}
echo "Your link has now been successfully submmited THANK YOU!";
mysql_close($con);
}




?>


Ok heres the code for you works fine :clapping: i also sent it to your email hope this helps

 

 

 


<?php 

$CONFIG['DB_CONFIG'] = array(
'server'   => 'localhost',
'user'     => 'root',
'password' => '',
'database' => 'websitevoice_'
);


 $con = mysql_connect($CONFIG['DB_CONFIG']['server'],$CONFIG['DB_CONFIG']['user'],
$CONFIG['DB_CONFIG']['password']);
if(!$con){
die("Can't connect to database");
}
mysql_select_db($CONFIG['DB_CONFIG']['database'],$con);
function protect($string){
$string = mysql_real_escape_string($string);

return $string;

}



$url = protect($_POST['url']);
$ref = protect($_POST['ref_id']);


if($url && $ref){

$sql = "INSERT INTO web_addresses (`link_url`,`ref_id`) VALUES('".$url."','".$ref."')"; 

if(!mysql_query($sql,$con)){

die(mysql_error());
}
echo "Your link has now been successfully submmited THANK YOU!";

}




?>


 

 

You're still going to get errors.

 

Change

 


$url = protect($_POST['url']);
$ref = protect($_POST['ref_id']);

 

to what i showed you.

 

 

 

function protect($input)
{

if (isset($_POST[$input]))
{
return $_POST[$input];
}
else
{
return FALSE;
}
}


$url = protect('url');
$ref = protect('ref_id');

[/code]

You're still going to get errors.

 

Change

 


$url = protect($_POST['url']);
$ref = protect($_POST['ref_id']);

 

to what i showed you.

 

 

 



function protect($input)
{

if (isset($_POST[$input]))
{
return $_POST[$input];
}
else
{
return FALSE;
}
}


$url = protect('url');
$ref = protect('ref_id');

[/code]

Well considering that there were no errors found at all in the error log or otherwise i'd say u need to bump your Experience down from a web guru to intermidiate cheeres :clapping:

Well considering that there were no errors found at all in the error log or otherwise i'd say u need to bump your Experience down from a web guru to intermidiate cheeres :clapping:

Someone can't take being corrected.

 

error_reporting(E_ALL);

 

If you don't know the basics i think you should stop giving others advice.

 

Grow up.

error_reporting(E_ALL);

 

If you don't know the basics i think you should stop giving others advice.

 

Grow up.

 

Lol ur really laughable dude everyone i've gave advice to been happy with the work so if ur gonna run ur mouth take it somewhere else lol buh bye

Well considering that there were no errors found at all in the error log or otherwise i'd say u need to bump your Experience down from a web guru to intermidiate cheeres :clapping:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.