Jump to content

Small registration program using PHP/MySQL


zaneosak

Recommended Posts

Hello, I created a php program last week that took in a time from a drop down menu and a persons name from a text box and store it in a text file. Also when submitted the form echo'd the Time and Persons name or echo'd if either the time was already taken or the name was already taken. The names and times were stored in a text file.

 

I am trying to make the same program but using a MySQL database in place of the text file. I am kind of new using MySQL with PHP but thought maybe someone could steer me in the right direction. Basically transforming 1 PHP program into the same one with a different method. Below is the code from the working program using the text file and the new one I am currently working on.

 

Any help would be appreciated, but currently I am getting an "Call to undefined function: msysql_connect()" error but I also know the rest isnt going to work as totally intended.

 

Using just PHP working as inteded

<?

$time=$_POST["time"];
$person=$_POST["user"];

if (file_exists("register.txt")) 
{
    $fileCheck = file("register.txt");
}
else 	{
    	$fileCheck = array(); 
}

$file = fopen("register.txt", "a+");
$newperson = "$time , $person\n";

$infile = false;
foreach ($fileCheck as $line) 
{
    if (stristr($line, $time) !== false) 
{
        echo "The timeslot is taken";
        $infile = true;
        }

    if (stristr($line, $person) !== false) 
{
        echo "The name is taken";
        $infile = true;
        }
}    

if (!$infile) {
    fwrite($file, $newperson);

echo "<br>Time: $time";
echo "<br> Name: $person";

}

?>

 

Attempting to use MySQL to do the same thing:

<?


$time=$_POST["time"]; 
$person=$_POST["user"];

$conn = msysql_connect("localhost", "username");
if (!$conn)
die("Error in connecting to MySQL database server");

@mysql_select_db("database", $conn) or
die("Error in selecting database");

$addrecord = "INSERT INTO regtable (Time, User) VALUES (".$_POST["time"]." , ".$_POST["user"].")";

$report = mysql_query($addrecord, $conn);

if(!$report) {
die("Error in retrieving information");
     }

if($report) {
echo "<br>Time: $time";
echo "<br>Name: $person";
            }


?>

 

Link to comment
Share on other sites

well that was rather dumb,

 

thanks for the heads up.

 

I fixed it and my output is my error message here:

if(!$report) {
die("Error in retrieving information");
     }

which I am guessing means its not inserting the information into the table at all, am I correct in that assumption?

 

Link to comment
Share on other sites

Not letting me edit my original post but here is the code that actually seems to output something, but all I get is my error that the record wasnt added to the table in the database

 

<?


$time=$_POST["time"]; 
$person=$_POST["user"];

$conn = mysql_connect("localhost", "username");
if (!$conn)
die("Error in connecting to MySQL database server");

@mysql_select_db("database", $conn) or
die("Error in selecting database");

$addrecord = "INSERT INTO regtable (Time, User) VALUES (".$_POST["time"]." , ".$_POST["user"].")";

$report = mysql_query($addrecord, $conn);

if(!$report) {
die("Error in retrieving information");
     }

if($report) {
echo "<br>Time: $time";
echo "<br>Name: $person";
            }


?>

 

Output: Error retieving information

Link to comment
Share on other sites

I think you have to quote you values in your SQL Query IE...

$addrecord = "INSERT INTO regtable (Time, User) VALUES ('".$_POST["time"]."' , '".$_POST["user"]."')";

 

try that ... A little trick i sometimes use when im having troubles with SQL is to echo out the statement then run it on in the database itself to see if it's processing and if not it will tell you where the error is

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.