Php/Sql

Haven’t touched php out for years, trying to help a friend out and struggling!

He already has php/sql running for other things so this seemed the right route to follow.

The idea is to be able to track deposits.

Atm I’ve set the database up and created a html entry form leading to php script

[php]<?php
$con=mysqli_connect(“localhost”,“depo”,“depo”,“deposits”);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$name = mysqli_real_escape_string($con, $_POST[‘name’]);
$unit = mysqli_real_escape_string($con, $_POST[‘unit’]);
$item = mysqli_real_escape_string($con, $_POST[‘item’]);
$depo = mysqli_real_escape_string($con, $_POST[‘depo’]);
$odate = mysqli_real_escape_string($con, $_POST[‘odate’]);
$rdate = mysqli_real_escape_string($con, $_POST[‘rdate’]);

$sql=“INSERT INTO depos (name, accom, item, deposit, odate, turn, rdate)
VALUES (’$name’, ‘$unit’, ‘$item’, ‘$depo’, ‘$odate’, ‘no’, ‘$rdate’)”;

if (!mysqli_query($con,$sql)) {
die('Error: ’ . mysqli_error($con));
}
echo “1 record added”;

mysqli_close($con);
?>[/php]

I’m struggling with my next stages which would be.

  1. showing the ID after entry (col 1,‘ID’, mediumint(9) , AUTO_INCREMENT) been playing with mysql_insert_id but can’t seem to get it right :confused:

  2. opening up and editing entrys, either by ID number or viewing list

  3. using SUM to calculate value of all under deposit only IF turn = ‘no’

Any help muchly appreciated!

Cheers, Rory

You should definitely add the Auto Increment Column and make that the Primary Key.

Playing with mysql_insert_id but can't seem to get it right :/

Try the below…

[php]if (!mysqli_query($con,$sql)) {
die('Error: ’ . mysqli_error($con));
}
echo $mysqli->insert_id;
echo “1 record added”;[/php]

In general you should do this, but dealing with money is a special kind of security risk… Use http://php.net/manual/en/mysqli.quickstart.prepared-statements.php prepared statements!

Sponsor our Newsletter | Privacy Policy | Terms of Service