-1

Im trying to insert some data into a mysql database. But I can not get it to work..

This is my reg.php file:

<?php
include 'mysql.php';
if (isset($_POST['submit']))
{
$email = $_POST["email"];
$sql = ("INSERT INTO users (`email`) VALUES('".$email."') ");
mysql_query($sql);
}
?>
<form action="" method="post"> <br />
<input type="text" name="email">
<input type="submit" name="submit"  >
</form>

And this is my mysql.php file:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}


echo 'Connected to MySQL<br>';
mysql_close($conn);
?>

And this is my table from phpmyadmin:

CREATE TABLE `users` (
`ID` int(11) NOT NULL,
`email` varchar(25) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

So any tips here to how i can solve this?

Thanks!

  • 1
    Why in 2017 are people still using the old, dead MySQL extension instead of MySQLi or PDO? And learn how to protect against SQL injection now, before you have to worry about it for real, and then it will become second nature – Mark Baker Feb 24 '17 at 11:33
  • When working on old machine without the control of the PHP version... – user2342558 Feb 24 '17 at 11:34
  • 1
    @user2342558 - Then the provider of the machine should be upgrading you to newer versions of PHP, not forcing you to continue with older, unsupported versions – Mark Baker Feb 24 '17 at 11:35
  • Completely accordingly with you :) – user2342558 Feb 24 '17 at 11:36
  • This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. – JoshulSharma Feb 24 '17 at 11:37
  • Not everybody is aware of that mysql is not supported anymore in some php verisons, but I have now updated it to MySQLi. Thaks for the tip! –  Feb 24 '17 at 11:44
  • 1
    Well done @Naffis - next step, [learning how to use prepared statements with bind variables](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Mark Baker Feb 24 '17 at 11:45
  • @MasivuyeCokile, no need for that comment – davejal Feb 24 '17 at 11:56
  • @MasivuyeCokile you are right that im not a developer, but i still want to learn and have fun with it.. or cant I do that, since im not a developer? –  Feb 24 '17 at 12:02
  • @Naffis - the speed with which you accepted my comments about switching from the MySQL interface to MySQLi shows that you're well on the path to being a developer... a large part of being a developer is a readiness to learn and keep learning (throughout your career and life) – Mark Baker Feb 24 '17 at 12:03
  • P.S. switch your database table charset (and collation) to UTF-8 and have a read of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) and you'll be better placed to handle characters from non-latin1 character sets – Mark Baker Feb 24 '17 at 12:08
  • @MarkBaker sorry about that, I just did not like how he responded. *Not everybody is aware of that mysql is not supported* Comment removed – Masivuye Cokile Feb 24 '17 at 12:09
  • @MarkBaker Thanks, will take a look at that. –  Feb 24 '17 at 12:23

2 Answers2

2

As someone said in updated version of php mysql_* function are deprecated you can use mysqli_* functions, your code is not working because you have closed your mysqli connection in mysql.php

<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   $dbname = "dbname";
   $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);//Put you db name here
   if(! $conn )
   {
      die('Could not connect: ' . mysqli_error());
   }


   echo 'Connected to MySQL<br>';
   //mysqli_close($conn); comment this line you code will work fine
?>

Thanks
Hope! This will help you

Shishil Patel
  • 3,449
  • 2
  • 12
  • 16
0

MySQl functions have been deprecated and try to put this in your code

mysql_query($sql);

after this

mysql_close();

also check your XAMPP/PHP version as from PHP 7 MYSQL function has been deprecated

Adi
  • 186
  • 9
  • I think you mean before, meaning he should remove it from the connection file `mysql.php` and place it somewhere after `mysql_query($sql);` to close the connection, But I don't like the answer at all we should not provide answers to deprecated functions, they are deprecated for a reason. – davejal Feb 24 '17 at 12:00
  • @davejal there is a reason why we all learn history and sometimes follow it. You just can't tell anyone for what they should do and should not. If the person wants to use it then its his/her choice completely. – Adi Feb 25 '17 at 19:12
  • The person asked a doubt and i replied along with updating about technology. – Adi Feb 25 '17 at 19:19