4 Replies - 546 Views - Last Post: 16 November 2012 - 01:58 AM Rate Topic: -----

#1 bs_72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 12

php forms and mysql querying

Posted 15 November 2012 - 02:59 AM

I'm beginning mysql and php, I'm trying to get this php file to add values to a mysql database. The problem is when I run the query I keep getting an error, I'm following what the book I purchased said but all I get is a server cannot be found erro


<html> 
<head> 
  <title>Incidence Reporting</title> 
</head> 
<body> 
  <h2>Incidence Reporting</h2> 
<?php 
  
  $db_s = "localhost";
$db_u = "bs_72";
$db_p = "tmmd2f";
$db_n = "bs_72";
$co = mysqli_connect($db_s,$db_u,$db_p)
or die('Error connecting to MySQL server.');

mysql_select_db($db_n);

  
  $first_name = $_POST['firstname'];
  $last_name = $_POST['lastname'];
  $e_mail = $_POST['email'];
  $when_it_happened = $_POST['whenithappened']; 
  $phone_number = $_POST['phonenumber']; 
  $i_description = $_POST['idescription']; 

  
  
  $query = “INSERT INTO las_cruces (first_name, last_name, e_mail, when_it_happened, “ .
  “phone_number, i_description) “ .
  “VALUES (‘$first_name’, ‘$last_name’,‘$e_mail’, ‘$when_it_happened’ , ‘$phone_number’, “ .
  “’$i_description’)”;
  

  
  $result = mysql_query($co, $query);
  or die('Error querying database.');
  
    echo 'Hello, ' . $first_name . ' ' . $last_name . '<br />';
  echo 'Thanks for submitting the form.<br />'; 
  echo 'Your email is ' . $e_mail . '<br />'; 
  echo 'Your phone number? ' . $phone_number . '<br />';
  echo 'The date of the incidence was ' . $when_it_happened . '<br />'; 
  echo 'What Happened: ' . $i_description . '<br />';


  
 
   
 
?> 
</body> 
</html>

<form method="post" action="report.php"> 



Is This A Good Question/Topic? 0
  • +

Replies To: php forms and mysql querying

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: php forms and mysql querying

Posted 15 November 2012 - 06:19 AM

You seem to be mixing up the MySQLi and MySQL extensions. You connect using MySQLi but then select the database using MySQL.

I suggest you cange the latter. MySQLi is better.
Was This Post Helpful? 1
  • +
  • -

#3 bs_72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 12

Re: php forms and mysql querying

Posted 15 November 2012 - 05:04 PM

<html> 
<head> 
  <title>Incidence Reporting</title> 
</head> 
<body> 
  <h2>Incidence Reporting</h2> 
<?php 
  
$db_s = "localhost";
$db_u = "bs_72";
$db_p = "tmmd2f";
$db_n = "bs_72";
$co = mysqli_connect($db_s,$db_u,$db_p)
or die('Error connecting to MySQL server.');

mysqli_select_db($db_n);

  
  $first_name = $_POST['firstname'];
  $last_name = $_POST['lastname'];
  $e_mail = $_POST['email'];
  $when_it_happened = $_POST['whenithappened']; 
  $phone_number = $_POST['phonenumber']; 
  $i_description = $_POST['idescription']; 

  
  
  $query = “INSERT INTO las_cruces (first_name, last_name, e_mail, when_it_happened, “ .
  “phone_number, i_description) “ .
  “VALUES (‘$first_name’, ‘$last_name’,‘$e_mail’, ‘$when_it_happened’ , ‘$phone_number’, “ .
  “’$i_description’)”;
  

  
  $result = mysqli_query($co, $query);
  or die('Error querying database.');
  
  echo 'Hello, ' . $first_name . ' ' . $last_name . '<br />';
  echo 'Thanks for submitting the form.<br />'; 
  echo 'Your email is ' . $e_mail . '<br />'; 
  echo 'Your phone number? ' . $phone_number . '<br />';
  echo 'The date of the incidence was ' . $when_it_happened . '<br />'; 
  echo 'What Happened: ' . $i_description . '<br />';


  
 
   
 
?> 
</body> 
</html>

<form method="post" action="report.php"> 



<p>Share where you saw the suspicous activity:</p> 
<form method="post" action="submit.php">

  <label for="firstname">First name:</label> 
  <input type="text" id="firstname" name="firstname" /><br /> 
  
  <label for="lastname">Last name:</label> 
  <input type="text" id="lastname" name="lastname" /><br /> 
  
  <label for="email">What is your email address?</label>
  <input type="text" id="email" name="email" /><br /> 
  
  <label for="whenithappened">When did it happen?</label> 
  <input type="text" id="whenithappened" name="whenithappened" /><br /> 
 
  <label for="phonenumber">What is your phone number?</label> 
  <input type="text" id="phonenumber" name="phonenumber" /><br /> 
  
  <label for="idescription">Describe what happened:</label> 
  <input type="text" id="idescription" name="idescription" size="100" /><br /> 
  

  
  <input type="submit" value="Report" name="submit" /> 
</form>



Ok I fixed what you said, but it's still not working, I also included my html form, why isn't working still :(
Was This Post Helpful? 0
  • +
  • -

#4 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 241
  • View blog
  • Posts: 700
  • Joined: 24-November 08

Re: php forms and mysql querying

Posted 15 November 2012 - 07:56 PM

Are you still getting a server not found error? Do you see your form at all? Are you testing this on your own machine?

If so, make sure you have php, apache and mysql installed. If you're starting out then it's best to use a collection like XAMPP as it handles a lot of the configuration for you and is well documented.

If you are using an online hosting service, make sure they host with php.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: php forms and mysql querying

Posted 16 November 2012 - 01:58 AM

there is no need to use mysqli_select_db(), you can pass the DB name as 4th parameter in mysqli_connect().

while I’m at it: line #28, wrong quote type (hint, strings would appear blue)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1