Jump to content

Inputs a 0 instead of username in field


walshybiker

Recommended Posts

Hi I'm new to this and am struggling with an assignment.

 

I want to populate a database (in phymyadmin) called event.  When a link is clicked it then moves to this page where I want it to populate a field called eventa with the username.  It just sends a 0 to all fields.

 

The php code is below:

 

 

<?php

 

//This checks that the user is registered and has entered the correct details

session_start();

 

// check if logged in or not?

if (!isset($_SESSION['username'])

    ||$_SESSION['username']=$username) {

 

    // not logged in, move to login page

    header('Location: login.php');

 

}

else {

$con=mysql_connect("localhost", "20154162", "lynne");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

 

mysql_select_db ("two0154162") or die (mysql_error());

$_POST[$_SESSION['username']=$username;

 

//insert fields into databse

$query = "INSERT INTO event(eventa)

VALUES ('"$_SESSION ["username"]'")";

 

mysql_query($query) or die('Error, insert query failed'.mysql_error());

 

//after login it returns to events page

// header('location:events.html');

// exit;

mysql_close ($con);

}

//echo ("got here");

 

?>

 

 

Thanks

Walshybiker

Link to comment
https://forums.phpfreaks.com/topic/202933-inputs-a-0-instead-of-username-in-field/
Share on other sites

I'm surprised you don't get an error message :)

 

 $_POST[$_SESSION['username']=$username;

 

Also, are you trying to compare or set the value here? (Do you need two equal signs?)

if (!isset($_SESSION['username']) 
    ||$_SESSION['username']=$username) {

 

Edit: Don't know how I missed it, but your query is wrong as well.

$query = "INSERT INTO event(eventa)
      VALUES ('"$_SESSION ["username"]'")";

It should be

$query = "INSERT INTO event(eventa)
      VALUES ('".$_SESSION ['username']."')";

 

Hope that helps,

-Kalivos

 

 

<?php

//This checks that the user is registered and has entered the correct details
session_start();

// check if logged in or not?
if (!isset($_SESSION['username']) 
    ||$_SESSION['username']==$username) {

    // not logged in, move to login page
    header('Location: login.php');
   
}
else {
$con=mysql_connect("localhost", "20154162", "lynne");
if (!$con)
{
		die('Could not connect: ' . mysql_error());
}

mysql_select_db ("two0154162") or die (mysql_error());
$_POST[$_SESSION['username']=true;

//insert fields into databse
	$query = "INSERT INTO event(eventa)
	VALUES ('"$_SESSION ["username"]"')";

	mysql_query($query) or die('Error, insert query failed'.mysql_error());

	//after login it returns to events page
//	header('location:events.html');
//		exit;
	mysql_close ($con);
}
echo ("got here");

?>

 

Edit: We have


tags for posting code within.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.