After pulling my hair I am close to giving up. Ive stared at the code for hours but I just cant see whats wrong. I have searched the net for similar faults but...it just dont make any sense to the fault I have (or rather the row the fault is said to be on).
Im new to the PHP coding buy are fairly confident in both html and css. To get to know the PHP coding Im now taking a distancecourse (BAD MISTAKE - I have nobody to ask if something flops belly up!!) so its many trial and errors (you learn by that too but...much hairpulling).
Anyway - to my problem. Im trying to do a "simple" webshop "thing" and are following a tutorial in the book thats in this course. But it just wont work for me... I have phpMyAdmin 3.4.5, use XAMPP as local server and Crimson Editor as...well...my code editor.
The database is named "webshop" and have 3 tables(and have these "tables" in them); customer ( cid, fname, lname, adress, postno, city, phone, login, password ), orders (oid, cid, pid, quantity), products (pid, itemno , prodtitle, descr ). The files are named shop.php and newreg.php.
The error that I get when I try and test "shop.php" is "Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\project\shop.php on line 41". LINE 41 in shop.php is the following: "mysql_select_db("webshop");" (as you can see for yourself).
When I try and check if the "newreg.php" work I can at least see that but it dont insert anything in the database.
So...where the heck (pardon my "french") is the fault....its probably something tinyweeny thing but...I just cant get it to work.
Also...I would also, if possible, have some help with how to make a sort of "admin" thing so I can ADD, CHANGE and DELETE things in my shop (like a admin section) without using phpMyAdmin.
I know you guys probably get that question a million times over but...if you dont ask...Thank you a lot in advance!
shop.php
<?
session_register("cart");
?>
<html>
<head>
<title>Shop</title>
</head>
<body>
<?
if(isset($_POST['ok'])){
Add();
}
if(isset($_POST['remove'])){
Remove();
}
if(isset($_GET['action'])||isset($_POST['action'])){
if($_GET['action']=='login'){
LogIn();
}
if($_POST['action']=='insert'){
Insert();
}
}else{
ShowProducts();
ShowContent();
}
?>
</body>
</html>
<?
function ShowProducts() {
$link = mysql_connect ("localhost", "root", "")
mysql_select_db("webshop");
$sql="SELECT * FROM products";
$result = mysql_db_query("products", "$sql");
echo "<table>";
echo "<tr><th>Item<th><th>Quantity<th><th><th></tr>";
while($row=mysql_fetch_array($result)){
echo "form method='post' action='shop.php'">;
echo "<tr><td>";
echo $row["descr"];
echo "<input type='hidden' name='descr' value='".$row["descr"]."'>";
echo "</td><td><input type='text' name='quantity'></td>";
echo "<td><input type='hidden' value='".$row['pid']."' name='id'>";
echo "<input type='submit' value='Add to Cart' name='ok'></td></tr></form>";
}
mysql_close ($link);
echo "</table>";
}
?>
function add() {
$quantity = $_POST['quantity'];
$id = $_POST['id'];
$descr = $_POST['descr'];
if(!empty($_POST['ok'])) {
if(!SESSION['cart'][$_id]["id"])
{
$_SESSION['cart'][$id]["id"]=$id;
$_SESSION['cart'][$id]["quantity"]=$quantity;
}else{
$_SESSION['cart'][$id]["quantity"]=$_SESSION['cart'][$id]["quantity"]+$quantity;
}
}
}
function ShowContent() {
if(!empty($_SESSION['cart'])) {
echo "<br/> You order contains the following:"<br/>;
echo "<table>";
echo "<tr><th>Item</th><th>Quantity</th><th></th></tr>";
foreach($_SESSION['cart'] as $p){
if(!$p["quantity"]==0){
echo "<form method='post' action='shop.php'";
echo "<tr><td>".$p["descr"]."</td><td>".$p["quantity"]."</td></td>";
echo "<input type='hidden' value="'.$p["id"].'" name"id">;
echo "input type='submit' value='Remove' name='remove'></tr>";
echo "</form>";
}
}
echo "</table>";
echo "<a href='shop.php=action=login'">Go to CheckOut</a>";
}
}
function remove()
$id=$_POST['id']:
$_SESSION['cart'][$id]["quantity"]=0:
}
function Login(){
echo "Submit your login and password if you are a returning customer<br/>;
echo "form method='post' action='shop.php'>";
echo "Login:<br/><input type='text' name='login'><br/>";
echo "Password:<br/><input type='password' name='password'><br/>";
echo "input type='submit' name='auction' value='Send'>";
echo "<a href='newreg.php'>New Customer? Sign up here!</a>"
}
function Insert(){
$login = $_POST['login'];
$password = $_POST['password'];
$link = mysql_connect ("localhost", "root", '');
$sql="SELECT * FROM customer WHERE login='$login' AND password='$password'";
$result = mysql_db_query("products", "$sql");
if(!($row=mysql_fetch_array($result))){
echo "Wrong Login";
echo "Please try again!";
LogIn();
]
else {
$cid = $row['cid'];
$link = mysql_connect ("localhost", "root", '');
foreach($_SESSION['cart'] as $p){
if(!$p["quantity"]==0){
$pid = $p["id"];
$quantity = $p{"quantity"];
$sql=INSERT INTO orders(cid, pid,quantity) VALUES ($cid,$pid,$quantity)";
my_sql_query("products", "$sql");
}
}
session_unset();
echo "Thank you for your order<br/> Welcome back<br/>";
echo "<a href="shop.php">Back to the store</a>";
mysql_close($link);
}
</body></html>
newreg.php
<?
if(isset($_POST['check'])) {
Check();
}
else {
Showform();
}
function Showform() {
?>
<table>
<form action="newreg.php" method="post" name='reg' OnSubmit="return send_OnClick();">
<tr><td>Firstname: </td><td><input type="text" name="fname"></td></tr>
<tr><td>Lastname:</td><td> <input type="text" name="lname"></td></tr>
<tr><td>Adress: </td><td><input type="text" name="adress"></td></tr>
<tr><td>Postno: </td><td><input type="text" name="postno"></td></tr>
<tr><td>City: </td><td><input type="text" name="city"></td></tr>
<tr><td>Phone: </td><td><input type="text" name="phone"></td></tr>
<tr><td>Login: </td><td><input type="text" name="login"></td></tr>
<tr><td>Password: </td><td><input type="password" name="password"></td></tr>
<tr><td></td><td><input type='reset' value='Reset'>
<input type='submit' name='send' value='Send'></td></tr>
</form>
</table>
<?
}
function Check() {
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$adress=$_POST['adress'];
$postno=$_POST['postno'];
$city=$_POST['city'];
$phone=$_POST['phone'];
$login=$_POST['login'];
$password=$_POST['password'];
$link = mysql_connect ("localhost", "root","");
$sql="SELECT * FROM customer WHERE login='$login' AND password='$password'";
$result = mysql_db_query("products","$sql");
if(!($row=mysql_fetch_array($result))) {
$sql="INSERT INTO customer(fname,lname,adress,postno,city,phone,login,password)";
$sql=$sql."VALUES('$fname','$lname','$adress','$postno','$city','$phone','$login','$password')";
mysql_db_query("products","$sql");
echo "Congrats - You are now registrered as client.<br>";
echo "<a href='shop.php?action=login'>Back</a>";
}
else {
echo "Sorry, your username was taken, please use another.";
Showform();
}
mysql_close ($link);
}
?>
<script language="JavaScript">
function send_OnClick() {
var los = document.reg.password.value;
var log = document.reg.login.value;
if((los.length != 0) && (log.length != 0)) {
return true;
} else {
alert("You must login to be able to shop");
return false;
}
}
</script>