Jump to content

Steven

Members
  • Posts

    150
  • Joined

  • Last visited

Posts posted by Steven

  1. Oi. Having a heck of a time with this. Here's the error:

    Notice: Undefined index: clientid in C:xampphtdocsmos-loginsertJobs.php on line 14Notice: Undefined index: clientname in C:xampphtdocsmos-loginsertJobs.php on line 14Error: Cannot add or update a child row: a foreign key constraint fails (`moslog`.`jobs`, CONSTRAINT `jobs_ibfk_1` FOREIGN KEY (`clientid`) REFERENCES `clients` (`id`))

    Here's my job form page:

    <div class="formContainer">	<form action="insertJobs.php" method="post">	<p>		<label>Client:</label>		<?php			$result = mysqli_query($con,"SELECT * FROM clients");			echo "<select name='name'>";			while ($row = mysqli_fetch_array($result)) {				echo "<option value='" . $row['clientid'] . "'>" . $row['name'] . "</option>";			}			echo "</select>";    ?>    <br>		<label>Date Received:</label> 		<input type="date" name="datein" class="focus-glow datepicker job--dateinInput" size="28">    <span class="job--dateinHint">Date placed</span><br>				<label>Description:</label> 		<textarea type="text" name="description" class="focus-glow"  size="28"></textarea>				<p>				<input type="submit" label="Submit">				<p>	</form>

    here's the job insert page:

    <?php require('includes/config.php'); ?><!doctype html><html lang="en"><head>  <meta charset="utf-8">  <title>Custom Project Management System</title>  <link rel="stylesheet" href="style.css"></head><?php	$sql="INSERT INTO jobs (clientid, clientname, datein, description)		VALUES 		('$_POST[clientid]','$_POST[clientname]','$_POST[datein]','$_POST[description]')";		if (!mysqli_query($con,$sql))		{			die('Error: ' . mysqli_error($con));		}		echo "1 record added";		mysqli_close($con);?><p><a href="formJobs.php">Add another job</a></p><p><a href="resultsJobs.php">View results</a></p>

    I'm coming to my wit's end =

  2. Louis Lazaris has a good article at his blog, Impressive Webs, where he basically comes to the conclusion that the introduction of <section> and <article> in HTML5 pretty much just muddies the waters:

     

     

    In short, HTML5′s <section> element could be used in any instance where it contains standalone, non-syndicated, non-aside content whose wrapper is not for styling purposes. In other words: Hardly ever.

     

    I think using <article> for individual posts would be okay, but for everything else I'd probably use a <div> with appropriate class names. In the end, though, I really don't think it matters much.

    • Like 1
  3. Ok, so I started out again with a separate, new database. Just to start fresh after trying out so many things. I'm having trouble understanding how to POST data to two related tables. Here are my tables:

    Table: clients--------------------------|  id  (pk, ai) |  name  | --------------------------

    Clients has two columns: "id" and "name". "id" is the primary key and it auto-increments. "name" is a simple varchar for the client's name.

    Table: jobs--------------------------------------------------------------------------|  clientid (fk) |  clientname (fk) | jobid (u, ai) | date | description | --------------------------------------------------------------------------

    The jobs table is the child of clients. "clientid" is a foreign key that points to clients.id, as well as "clientname" is a foreign key that points to clients.name. "jobid" is a unique, auto-incremented key and is used to distinguish all jobs from eachother. "date" and "description" are pretty simple.

     

    On the page that has the form for submitting new jobs, I have fields for "Client Name", "Date" and "Description". In this case, I want the database to assign a unique number to the jobid (preferably a random 5-digit number, but that's not my concern right now). What I've been getting hung up on for quite some time now is the "Client Name" field. In the insert.php file, exactly how do I handle the relationship between the two tables in my INSERT command? I need a bit more help being walked through how to join the tables.

     

     

    Thank you in advance!

  4. Okay, yet another followup question. I'm guessing I have a sql "architecture" problem.

     

    So I have separate tables for clients and jobs. I have successful pages (resultsClients.php and resultsJobs.php) that display all entries for those two tables. And, I've successfully made it so that, for example, on the resultsClients.php page you can click a client's name and it will bring you to viewclient.php and will display that client's name and details. I also have done that for the resultsJobs.php page where you can click on any of the jobs' unique job number link and it will bring you to viewjob.php and it will show you that particular job's detailed information.

     

    Going great so far.

     

    But, on the resultsJobs.php page, I want the name of the client for whom each job is for to be linked to viewclient.php as well. Problem is, after I make the link, each client on resultsJobs.php has a clientid of 1, and not its unique id that it is supposed to have.

     

    I think my real question is: when the user fills out the job form page, how do I link the client text field with the client table? Code is below:

     

    Here's the form from "formJobs.php"

    <div class="formContainer">	<form action="insertJobs.php" method="post">	<p>		<label>Client:</label> 		<input type="text" name="client" class="focus-glow job--clientInput auto" size="28">                <span class="job--clientHint">Enter the client name</span>    <br>				<label>Job Number:</label> 		<input type="text" name="jobnumber" class="focus-glow job--jobnumberInput" size="28">		<span class="job--jobnumberHint">DSI job number</span><br>		<label>Status:</label> 		<input type="text" name="status" class="focus-glow job--statusInput" size="28">		<span class="job--statusHint">Delivery status</span><br>		<label>Date Received:</label> 		<input type="date" name="datein" class="focus-glow datepicker job--dateinInput" size="28">    <span class="job--dateinHint">Date placed</span><br>				<label>Date Due:</label> 		<input type="date" name="datedue" class="focus-glow datepicker job--datebyInput" size="28">    <span class="job--datebyHint">Date due, if any</span><br>				<label>Date Out:</label> 		<input type="date" name="dateout" class="focus-glow datepicker job--dateoutInput" size="28">    <span class="job--dateoutHint">Date shipped</span><br>				<label>Completed:</label> 		<input type="text" name="completed" class="focus-glow job--completedInput" size="28">    <span class="job--completedHint">Yes / No</span><br>				<label>PO Number:</label> 		<input type="text" name="po" class="focus-glow job--ponumberInput" size="28">    <span class="job--ponumberHint">Client PO, if any</span><br>				<label>Description:</label> 		<textarea type="text" name="description" class="focus-glow"  size="28"></textarea>		<p>		<input type="submit" label="Submit">		<p>	</form>	<p class="viewResults"><a href="resultsJobs.php">View results</a></p></div><!-- /form container -->

    And "resultsJobs.php":

    <?php require('includes/config.php'); ?><!doctype html><html lang="en"><head>  <meta charset="utf-8">  <title>Custom Project Management System</title>  <link rel="stylesheet" href="style.css"></head><body><div id="container">	<!-- Include Navigation -->	<?php include ('includes/header.php'); ?>	<!-- Include Navigation -->	<?php include ('includes/navigation.php'); ?><h2>Job Log</h2><?phpecho "<table><tr><th>Client</th><th>Job No.</th><th>Status</th><th>Date In</th><th>Date Due</th><th>Date Out</th><th>Completed</th><th>Description</th><th>Purchase Order</th></tr>";$result = mysqli_query($con,"SELECT * FROM jobs, clients GROUP BY jobnumber");while($row = mysqli_fetch_array($result)){	echo "<tr>";	echo '<td><a href="viewclient.php?clientid='.$row['clientid'].'">'.$row['name'].'</a></td>';	echo '<td><a href="viewjob.php?jobnumber='.$row['jobnumber'].'">'.$row['jobnumber'].'</a></td>';	echo "<td>" . $row['status'] . "</td>";	echo "<td>" . $row['datein'] . "</td>";	echo "<td>" . $row['datedue'] . "</td>";	echo "<td>" . $row['dateout'] . "</td>";	echo "<td>" . $row['completed'] . "</td>";	echo "<td>" . $row['description'] . "</td>";	echo "<td>" . $row['po'] . "</td>";	echo "</tr>";}echo "</table>";?></div><!-- / main container --></body></html>
  5. Okay, what parts? How do I fix it? I'm new to both PHP and SQL (if you couldn't tell already ;)).

     

    This is only on a local machine, won't be uploaded to the internet at all. But I do agree, if there are potential threats, they should be avoided.

  6. Oh my word. Sorry.

     

    Turns out I had changed the <a> tag in the resultsClients.php to <a href="viewclient.php?clientid='.$row['clientid'].'">, but never refreshed the viewclient.php page I was on.

     

    Ha, well, it works now!

  7. Thanks for the help so far.

     

    But I'm having trouble with those links you gave me. They are dealing with static information, how do I deal with the id in the url changing?

     

    Here's what I have:

    	<?php		$id = $_GET['clientid'];		$result = mysqli_query($con,"SELECT name FROM clients WHERE clientid=$id");				while($row = mysqli_fetch_array($result))		{			echo '<h2>'.$_GET['$id'].$row['name'].'</h2>';		}	?>

    The page returns this error:

     

    Notice: Undefined index: clientid in C:...viewclient.php on line 25Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocsdsi-logviewclient.php on line 28

  8. Alright, I have two pages. "resultsClients.php" is the page that lists all clients in the db with links to the file "viewclient.php" that displays details about the specified client. I've been working on redoing some naming conventions, like file names, to make more sense. Anyway, here comes the code...

     

    resultsClients.php:

    <body><div id="container">	<!-- Include Navigation -->	<?php include ('includes/header.php'); ?>	<!-- Include Navigation -->	<?php include ('includes/navigation.php'); ?><h2>Clients</h2><?phpecho "<table><tr><th>Client</th></tr>";$result = mysqli_query($con,"SELECT * FROM clients");while($row = mysqli_fetch_array($result)){	echo "<tr>";	echo '<td><a href="viewclient.php?id='.$row['clientid'].'">'.$row['name'].'</a></td>';	echo "</tr>";}echo "</table>";?></div><!-- / main container -->

    I've made a config.php file located in an "includes" dir for handing db connection. In the db there is a table called "clients" with two columns for now: "clientid" and "name". the first column is an auto-incremented primary key.

     

    Here is viewclient.php:

    <div id="container">	<!-- Include Navigation -->	<?php include ('includes/header.php'); ?>	<!-- Include Navigation -->	<?php include ('includes/navigation.php'); ?>	<?php		$result = mysqli_query($con,"SELECT DISTINCT name FROM clients");		while($row = mysqli_fetch_array($result))		{			echo '<h2>'.$row['name'].'</h2>';		}	?></div><!-- /container -->

    In this page I know my $result is wrong for what I want to do, it's retrieving all the client names and basically forming a list of <h2>'s with all the client names. How do I make $result grab the clientid that is in the page url? I'm guessing you don't actually tell your PHP code to grab the id from the url, but that's the best way I can explain what I want.

  9. This is an old topic, but it's what I've been looking for.

     

    I have a question: on my "display.php" page, how do I, for example, make a <h2> element saying:

    <h2>"display id"</h2>

    In my case, I have a "viewclients.php" file that I want to pull information from depending on the certain client you clicked from the previous page. My <h2> element keeps returning all client names, when I want it to return only the name of the client based on the id in the url... does that make sense?

  10. I found a better error report snippet. It's giving me useful information, but not sure how to proceed. Here's my current code:

    <?php  mysqli_report(MYSQLI_REPORT_OFF); // Turn off default error msgs  $mysqli = new mysqli("host", "user", "pass", "dbname");  $query = "INSERT INTO clients (name) VALUES ('$_POST[name]')";  $res = $mysqli->query($query);    if ($mysqli->error) {      try {        throw new Exception("MySQL error $mysqli->error <br> Query:<br>$query", $mysqli->errno);      }      catch(Exception $e) {        echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br>";        echo nl2br($e->getTraceAsString());      }    }    else {      echo "Great! A new client has been added.";    }?>

    And I just realized in the middle of typing this that when I reset the clients table, I forgot to set the client id column to auto-increment. :) Fixed.

  11. I'm getting a strange error.

     

    I'm trying to add new clients, but it has been telling me the client already exists. I thought maybe the autoincrement was acting weird, so I dropped the table and started fresh. Still said the client already exists.

     

    I tried using the mysqli error report from the PHP manual, but I'm assuming I'm doing something wrong, because all it reports is:

    Errormessage: Unknown system variable 'a'

    Here's my current code:

    <?php	$con=mysqli_connect("host","user","pass","dbname");	// Check connection	if (mysqli_connect_errno()) {			printf("Connect failed: %sn", mysqli_connect_error());			exit();		}	$sql = "INSERT INTO clients (name) VALUES ('$_POST[name]')";	if (!mysqli_query($con, "SET a=1")) {		printf("Errormessage: %sn", mysqli_error($con));	}	else {		echo "Great! We've successfully added a new client.";	}		mysqli_close($con);?>
  12. Okay, another update.

     

    I went to the PHP Manual (RTF, amirite?). My suspicion, though I could be wrong, is that trying to use mysql while the rest of my code is using mysqli is causing some hangup. So I used this piece from the manual to find the error number occuring for mysqli:

    if (!mysqli_query($con, "SET a=1")) {  printf("Errorcode: %dn", mysqli_errno($con));}

    That gave me an error number of 1193, so then I tweaked the piece of code I got from the Stackoverflow answer from earlier and came up with this:

    if (!mysqli_errno() == 1193) {  print "That client already exists!";}

    It's working! Sort of... It does indeed print "That client already exists", but only after reporting "1 record added" and an error message:

    Warning: mysqli_errno() expects parameter 1 to be mysqli, string given in ..page.php on line 30

    I also tried changing "if (!mysqli_errno() == 1193" to "if (!mysqli_errno($con) == 1193"

  13. Whoops.

     

    After I sent it down it's still just returning the default error.

     

    Edit:

     

    This is interesting. I tried removing the following code:

    if (!mysqli_query($con,$sql))	{		die('Error: ' . mysqli_error($con));	}

    Making the code in full:

    <?php	$con=mysqli_connect("host","user","pass","db");	// Check connection	if (mysqli_connect_errno())		{			echo "Failed to connect to MySQL: " . mysqli_connect_errno();		}	$sql="INSERT INTO clients (name)		VALUES 		('$_POST[name]')";		if (mysql_errno() == 1062) {			print "That client already exists!";		}		echo "1 record added";		mysqli_close($con);?>

    I'm not getting this error message anymore: "error Duplicate entry for key". It's echoing "1 record added", ignoring the if mysql_errno statement. I looked at the DB itself, and it isn't making any duplicates, but I'm wondering why it isn't giving the error report I want it to?

     

     

    Thanks again for your help and patience, guys.

  14. Hmm, that answer looked promising. It's still giving me the default error, though.

     

    Here's my code:

    $sql="INSERT INTO clients (name)	VALUES 	('$_POST[name]')";	if (mysql_errno() == 1062) {		echo "That client already exists!";	}			if (!mysqli_query($con,$sql)) {		die('Error: ' . mysqli_error($con));	}	echo "1 record added";	mysqli_close($con);?>
  15. 	$sql="INSERT INTO clients (name)		VALUES 		('$_POST[name]')";		if (!mysqli_query($con,$sql))		{			die('Error: ' . mysqli_error($con));		}		echo "1 record added";		mysqli_close($con);?>

    I haven't put in any code to show an error on duplication, not sure how to go about that. But MySQL is apparently reporting its own error on the browser after I set the "name" column to UNIQUE.

  16. Excellent, thank you.

     

    Another follow-up question. After I made my separate Clients form and results page, I found that I was still able to add duplicates. I tried browsing Stackoverflow for an answer to avoiding duplicating, but couldn't find much help. Then I went into my phpmyadmin page and set the 'name' column to UNIQUE and it works. I tested adding a duplicate client and received an error saying there was a duplicate for "name". That's good, that's what I want, but how can I control that error message? Mainly for UX reasons, I want to control that error message so I can style it as well as give the user a helpful link back to the form.

     

     

    Many thanks

  17. Alright, that makes a lot of sense. I figured I had to change my database somehow.

     

    Right now the page where users input job log details, the form uses action="insert.php", I'm guessing with two tables (clients and jobs) I'd have two insert pages (something like insertClient.php and insertJob.php) that would send the inputed data to the correct tables?

  18. Hi there. Used to lurk around here a few years back, helping where I could in the HTML/CSS forum. Well, I'm back.

     

    I'm working on a basic, in-house job log form. My knowledge level in PHP and SQL is very low, so I'm kind of stumbling blind. I've managed to make a form, get it to send new information to my DB, and made a results page so you can see a table with all the submitted information. There are a number of features I want to add, but I'll save those for a later post.

     

    I made a drop-down list on the results page, here's the code:

    $result = mysqli_query($con,"SELECT client FROM jobs");echo "<select name='client'>";while ($row = mysqli_fetch_array($result)) {	echo "<option value='" . $row['client'] . "'>" . $row['client'] . "</option>";}echo "</select>";

    It's working fine. It makes a drop-down lists and populates it with all the clients from my DB.

     

    However, here's the problem:

     

    And I'm sure it has something to do with my DB structure. As of now, there are six or more "jobs" added to this joblog database. 3 jobs for "Client A", 1 job for "Client B" and 3 jobs for "Client C". The problem is the drop-down gets populated like this:

     

    Client A

    Client A

    Client A

    Client B

    Client C

    Client C

    Client C

     

    Not good. I want it to be populated like this:

     

    Client A

    Client B

    Client C

     

    The purpose of this drop-down list is so you can view all the clients in the database, hit one, and have all the jobs for that client returned to you. Somehow when the user submits a new job to the joblog, that client needs to be identified with a unique client?

     

    I have no idea how to continue!

     

    Thanks in advance,

    Steve

×
×
  • Create New...