Jump to content

INSERTING forms from webpage to mysql


drakecai

Recommended Posts

I have two webpages, login_success.php and formentered.php

login_success.php is a page where i am entering new information and enteredvalues.php is supposed to be a script to enter the information into mysql. So far I got the login_success page to redirect to enteredvalues.php when submit is clicked but the information in the fields isn't transferring to mysql and neither is the page redirecting back to login_success. Here are both my codes

 

Login_success.php

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html><title>ChronoServe - Saving Time</title>
<link href="style.css" rel="stylesheet" type="text/css">
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="container">
  <tr>
    <td><form name="form2" method="post" action="entervalues.php">
    <table width="335px" height="50%" border="1" align="center" cellpadding="0" cellspacing="0" class="centered">
      <tr>
        <td><table width="100%" border="0" align="center" cellpadding="3" cellspacing="10">
      <tr>
        <td colspan="2"><div align="center" class="font2">Activation Information</div></td>
      </tr>
      <tr>
        <td colspan="2"></td>
      </tr>
      <tr>
        <td width="40%" class="font3">First Name :</td>
        <td width="60%">          <div align="center">
            <input name="firstname" type="text" class="font3" id="firstname" />
            </div></td>
      </tr>
      <tr>
        <td class="font3">Last Name :</td>
        <td>          <div align="center">
            <input name="lastname" type="text" class="font3" id="lastname" />
            </div></td>
      </tr>
      <tr>
        <td height="28" class="font3">Phone Number :</td>
        <td>          <div align="center">
            <input name="pnumber" type="text" class="font3" id="pnumber" />
            </div></td>
      </tr>
      <tr>
        <td class="font3">Personnel Activated :</td>
        <td>          <div align="center">
            <input name="numbactivated" type="text" class="font3" id="numbactivated" />
            </div></td>
      </tr>
      <tr>
        <td height="37" colspan="2"></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
          <input name="insert" type="submit" class="font3" value="Submit" />
        </div></td>
      </tr>
    </table></td>
      </tr>
      </form>
    </table>
    </td>
  </tr>
</table>
</body>
</html>

 

Here is enteredvalues.php

<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$pnumber=$_POST['pnumber'];
$numberactivated=$_POST['numberactivated'];

// To protect MySQL injection (more detail about MySQL injection)
$firstname = stripslashes($firstname);
$lastname = stripslashes($firstname);
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$pnumber = stripslashes($pnumber);
$numberactivated = stripslashes($numberactivated);
$pnumber = mysql_real_escape_string($pnumber);
$numberactivated = mysql_real_escape_string($numberactivated);

$sql="INSERT INTO $tbl_name (firstname, lastname, pnumber, numberactivated,) VALUES ('$firstname','$lastname','$pnumber','$numberactivated')")or die(mysql_error());  
$r = mysql_query($sql);
if(!$r) {

    $err=mysql_error();

    print $err;
header("location:login_success.php");
    exit();
}
?>

Link to comment
Share on other sites

it's not redirecting back to login_success because your logic tells it to redirect to login_success on a failure.

$sql="INSERT INTO $tbl_name (firstname, lastname, pnumber, numberactivated,) VALUES ('$firstname','$lastname','$pnumber','$numberactivated')")or die(mysql_error());  
$r = mysql_query($sql);
if(!$r) {

    $err=mysql_error();

    print $err;
header("location:login_success.php");
    exit();
}

 

!$r means if $r fails. try this

$sql="INSERT INTO $tbl_name (firstname, lastname, pnumber, numberactivated,) VALUES ('$firstname','$lastname','$pnumber','$numberactivated')")or die(mysql_error());  
$r = mysql_query($sql);

//if success, redirec to success page
if($r) {
header("location:login_success.php");
exit();
} else { //else error
    $err=mysql_error();

    print $err;

    exit();
}

 

and as for it not inserting into the mysql, put this bit of code up the top and ensure the form values are being posted to the page

print_r($_POST);
exit();

 

you'll obviously want to remove that when the site goes live.

 

Link to comment
Share on other sites

First off, thanks for helping! Much appreciated.

 

But the problem still isn't solved.

Here is the revised code I got.

It still doesn't enter the forms into the mysql table and neither does it redirect.

 

<?php
print_r($_POST);
exit();
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$pnumber=$_POST['pnumber'];
$numberactivated=$_POST['numberactivated'];

// To protect MySQL injection (more detail about MySQL injection)
$firstname = stripslashes($firstname);
$lastname = stripslashes($firstname);
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$pnumber = stripslashes($pnumber);
$numberactivated = stripslashes($numberactivated);
$pnumber = mysql_real_escape_string($pnumber);
$numberactivated = mysql_real_escape_string($numberactivated);

$sql="INSERT INTO $tbl_name (firstname, lastname, pnumber, numberactivated,) VALUES ('$firstname','$lastname','$pnumber','$numberactivated')")or die(mysql_error());  
$r = mysql_query($sql);

//if success, redirec to success page
if($r) {
header("location:login_success.php");
exit();
} else { //else error
    $err=mysql_error();

    print $err;

    exit();
}
?>

Link to comment
Share on other sites

does it print out all the posted values from the form?

I'm baffled as to what route your page is taking...

 

You have a separate login form (main_login.php)? that is posted to where?

and then the user is meant to see login_success when?

 

the lines I said

print_r($_POST);
exit();

should only be used whilst you're diagnosing this problem, they will print out any values posted to the page from a form.

Link to comment
Share on other sites

Well i'm trying to have a main login screen for someone to login to. After logging in they will be checked by checklogin.php. Afterwards they will be directed to login_sucess in which they can enter their first name, last name, phone number, and number of people their inviting. What i'm having trouble with is having those 4 details entered into a mysql database. I have the login_success page which has all the proper forms and I had it redirect to formentered.php which it would then enter those form values into the mysql database. Sorry if its a bit confusing.

 

I think there is a problem in the login_success.php perhaps I placed the <form> wrong.

 

EDIT: No, it doesn't post any of the forms.

Link to comment
Share on other sites

just start with a simple form and use

print_r($_POST);

untill you get the values posting.

 

Also in your HTML code you have a table inside a table inside a table... I would change this to just be one table and have the <form> tags outside the table, much easier to diagnose any HTML problems.

i.e.

 

<form><table><tr><td></td></tr></table></form>

 

 

And you are posting the form to "entervalues.php" and from what you've written that page is actually called "enteredvalues.php"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.